summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFabrice <fabrice@schaub-dev.xyz>2026-02-10 09:32:35 +0100
committerFabrice <fabrice@schaub-dev.xyz>2026-02-10 09:32:35 +0100
commitccab86b21825ebc41f0887ea633ea28906b32a8e (patch)
tree60f378c3629c139f412d41a85142d1b48518e54f /src
parent86c09e193ed66420da48a7fd62678286b89dace2 (diff)
adding vector and eventloop implementation
Diffstat (limited to 'src')
-rw-r--r--src/events.cc19
-rw-r--r--src/events.h7
-rw-r--r--src/vec.cc83
-rw-r--r--src/vec.h38
-rw-r--r--src/wayclock.cc14
-rw-r--r--src/wlstate.cc21
-rw-r--r--src/wlstate.h2
7 files changed, 166 insertions, 18 deletions
diff --git a/src/events.cc b/src/events.cc
new file mode 100644
index 0000000..850d48f
--- /dev/null
+++ b/src/events.cc
@@ -0,0 +1,19 @@
+#include "events.h"
+#include "vec.h"
+
+bool wayc_eventloop_init(struct eventloop_s *loop) {
+ wayc_notnull(loop);
+
+ if (!wayc_wlstate_init(&loop->state))
+ return false;
+
+ loop->events = WAYC_VEC_INIT(struct event_s);
+ return true;
+}
+
+void wayc_eventloop_deinit(struct eventloop_s *loop) {
+ wayc_notnull(loop);
+
+ wayc_wlstate_deinit(&loop->state);
+ wayc_vec_deinit(&loop->events);
+}
diff --git a/src/events.h b/src/events.h
index 73efe55..8e27576 100644
--- a/src/events.h
+++ b/src/events.h
@@ -1,6 +1,6 @@
#pragma once
-#include "utils.h"
+#include "vec.h"
#include "window.h"
#include "wlstate.h"
@@ -15,5 +15,8 @@ struct event_s {
struct eventloop_s {
struct wlstate_s state;
-
+ vec_s<struct event_s> events;
};
+
+bool wayc_eventloop_init(struct eventloop_s *loop);
+void wayc_eventloop_deinit(struct eventloop_s *loop);
diff --git a/src/vec.cc b/src/vec.cc
new file mode 100644
index 0000000..d43bc52
--- /dev/null
+++ b/src/vec.cc
@@ -0,0 +1,83 @@
+#include "vec.h"
+#include "utils.h"
+#include <cstring>
+#include <mimalloc.h>
+
+static inline usize wayc_raw_vec_next_cap(usize curr_cap, usize grow_to) {
+ if (curr_cap == 0)
+ return WAYC_VEC_INITIAL;
+
+ usize next_cap = curr_cap * WAYC_VEC_GROWTH;
+ if (next_cap < grow_to)
+ return grow_to;
+
+ return next_cap;
+}
+
+static inline bool wayc_raw_vec_needs_grow(raw_vec_s *vec) {
+ wayc_notnull(vec);
+ wayc_assert(vec->len <= vec->cap);
+ return vec->len == vec->cap;
+}
+
+static inline usize wayc_raw_vec_bytes(usize size, usize value) {
+ return value * size;
+}
+
+static inline u8 *wayc_raw_vec_at(raw_vec_s *vec, usize index) {
+ wayc_notnull(vec);
+ u8 *at = vec->ptr + wayc_raw_vec_bytes(vec->size, index);
+ return at;
+}
+
+static void wayc_raw_vec_grow(raw_vec_s *vec, usize grow_to) {
+ wayc_notnull(vec);
+ wayc_assert(wayc_raw_vec_needs_grow(vec));
+
+ usize curr_bytes = wayc_raw_vec_bytes(vec->size, vec->cap);
+ usize next_cap = wayc_raw_vec_next_cap(vec->cap, grow_to);
+ usize next_bytes = wayc_raw_vec_bytes(vec->size, next_cap);
+
+ u8 *next_ptr = (u8 *)mi_malloc(next_bytes);
+ memcpy(next_ptr, vec->ptr, curr_bytes);
+ mi_free(vec->ptr);
+
+ vec->ptr = next_ptr;
+ vec->cap = next_cap;
+}
+
+void wayc_raw_vec_push(raw_vec_s *vec, const u8 *at) {
+ wayc_notnull(vec);
+ wayc_notnull(at);
+
+ if (wayc_raw_vec_needs_grow(vec))
+ wayc_raw_vec_grow(vec, vec->len + 1);
+
+ u8 *dst = wayc_raw_vec_at(vec, vec->len);
+ memcpy(dst, at, vec->size);
+ vec->len++;
+}
+
+bool wayc_raw_vec_pop(raw_vec_s *vec, u8 *out) {
+ wayc_notnull(vec);
+ wayc_notnull(out);
+
+ if (vec->len == 0)
+ return false;
+
+ u8 *at = wayc_raw_vec_at(vec, vec->len - 1);
+ memcpy(out, at, vec->size);
+ vec->len--;
+ return true;
+}
+
+void wayc_raw_vec_deinit(raw_vec_s *vec) {
+ wayc_notnull(vec);
+ if (vec->ptr == nullptr)
+ return;
+
+ mi_free(vec->ptr);
+ vec->ptr = nullptr;
+ vec->len = 0;
+ vec->cap = 0;
+} \ No newline at end of file
diff --git a/src/vec.h b/src/vec.h
new file mode 100644
index 0000000..2b4d11c
--- /dev/null
+++ b/src/vec.h
@@ -0,0 +1,38 @@
+#pragma once
+
+#include "utils.h"
+
+#define WAYC_VEC_INITIAL 4
+#define WAYC_VEC_GROWTH 2
+#define WAYC_VEC_THRESHOLD 8
+
+struct raw_vec_s {
+ u8 *ptr;
+ usize size;
+ usize len, cap;
+};
+
+#define WAYC_RAW_VEC_INIT(size) {nullptr, size, 0, 0}
+
+void wayc_raw_vec_push(raw_vec_s *vec, const u8 *at);
+bool wayc_raw_vec_pop(raw_vec_s *vec, u8 *out);
+void wayc_raw_vec_deinit(raw_vec_s *vec);
+
+template <typename T> struct vec_s {
+ raw_vec_s raw;
+};
+
+#define WAYC_VEC_INIT(type) {WAYC_RAW_VEC_INIT(sizeof(type))}
+
+template <typename T>
+static inline void wayc_vec_push(vec_s<T> *vec, const T *at) {
+ wayc_raw_vec_push(&vec->raw, (u8 *)at);
+}
+
+template <typename T> static inline bool wayc_vec_pop(vec_s<T> *vec, T *out) {
+ return wayc_raw_vec_pop(&vec->raw, (u8 *)out);
+}
+
+template <typename T> static inline void wayc_vec_deinit(vec_s<T> *vec) {
+ wayc_raw_vec_deinit(&vec->raw);
+} \ No newline at end of file
diff --git a/src/wayclock.cc b/src/wayclock.cc
index 7f46c40..6114cbe 100644
--- a/src/wayclock.cc
+++ b/src/wayclock.cc
@@ -1,14 +1,10 @@
-#include "wlstate.h"
+#include "events.h"
int main() {
- wlstate_s state;
- if (!wayc_wlstate_init(&state))
- wayc_panic("Failed to initialize Wayland state", nullptr);
+ eventloop_s loop;
+ if (!wayc_eventloop_init(&loop))
+ wayc_panic("Failed to initialize event loop");
- while (true) {
- wayc_wlstate_update(&state);
- }
-
- wayc_wlstate_deinit(&state);
+ wayc_eventloop_deinit(&loop);
return 0;
} \ No newline at end of file
diff --git a/src/wlstate.cc b/src/wlstate.cc
index 4b1a65f..68846fb 100644
--- a/src/wlstate.cc
+++ b/src/wlstate.cc
@@ -47,8 +47,9 @@ static void wayc_registry_add(void *data, struct wl_registry *registry,
static void wayc_registry_remove(void *data, struct wl_registry *registry,
u32 name) {
- wayc_assert(data != NULL);
- wayc_assert(registry != NULL);
+ wayc_notnull(data);
+ wayc_notnull(registry);
+
wayc_panic("Registry global removed: name=%u", name);
}
@@ -58,7 +59,7 @@ static struct wl_registry_listener WAYC_REGISTRY_LISTENER = {
};
bool wayc_wlstate_init(struct wlstate_s *state) {
- wayc_assert(state != NULL);
+ wayc_notnull(state);
wl_display_t display = wl_display_connect(NULL);
if (display == NULL)
@@ -87,16 +88,24 @@ bool wayc_wlstate_init(struct wlstate_s *state) {
}
void wayc_wlstate_deinit(struct wlstate_s *state) {
- wayc_assert(state != NULL);
+ wayc_notnull(state);
+
+ if (state->display == nullptr)
+ return;
wl_display_disconnect(state->display);
+ state->display = nullptr;
+ state->registry = nullptr;
+ state->compositor = nullptr;
+ state->wm_base = nullptr;
+ state->eventfd = -1;
}
void wayc_wlstate_update(struct wlstate_s *state) {
- wayc_assert(state != NULL);
+ wayc_notnull(state);
struct pollfd pfd = {
- (i32)state->eventfd,
+ state->eventfd,
POLLIN,
0,
};
diff --git a/src/wlstate.h b/src/wlstate.h
index 7bb3499..98885db 100644
--- a/src/wlstate.h
+++ b/src/wlstate.h
@@ -7,7 +7,7 @@ struct wlstate_s {
wl_registry_t registry;
wl_compositor_t compositor;
xdg_wm_base_t wm_base;
- u32 eventfd;
+ i32 eventfd;
};
bool wayc_wlstate_init(struct wlstate_s *state);