summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabrice <fabrice@schaub-dev.xyz>2026-02-10 10:17:45 +0100
committerFabrice <fabrice@schaub-dev.xyz>2026-02-10 10:17:45 +0100
commit55991c396cddc8ec27182dc5cfe64fdef61da064 (patch)
treee2e5b80a2d27d3d717a3be1e7240ecb5cd1b298c
parent80dcfa99fef3684d506ee8f96298563433b10e74 (diff)
turning hashset into hashmap
-rw-r--r--src/events.cc5
-rw-r--r--src/events.h5
-rw-r--r--src/hashm.h84
-rw-r--r--src/window.h2
-rw-r--r--src/wlstate.cc1
5 files changed, 75 insertions, 22 deletions
diff --git a/src/events.cc b/src/events.cc
index 850d48f..6a3190c 100644
--- a/src/events.cc
+++ b/src/events.cc
@@ -1,13 +1,18 @@
#include "events.h"
+#include "hashm.h"
#include "vec.h"
+#include <cstring>
bool wayc_eventloop_init(struct eventloop_s *loop) {
wayc_notnull(loop);
+ memset(loop, 0, sizeof(*loop));
if (!wayc_wlstate_init(&loop->state))
return false;
loop->events = WAYC_VEC_INIT(struct event_s);
+ wayc_hashmap_init(&loop->windows);
+
return true;
}
diff --git a/src/events.h b/src/events.h
index 8e27576..bdbe6d2 100644
--- a/src/events.h
+++ b/src/events.h
@@ -1,5 +1,6 @@
#pragma once
+#include "hashm.h"
#include "vec.h"
#include "window.h"
#include "wlstate.h"
@@ -15,7 +16,9 @@ struct event_s {
struct eventloop_s {
struct wlstate_s state;
- vec_s<struct event_s> events;
+ struct vec_s<struct event_s> events;
+ window_id_t winid;
+ struct hashmap_s<window_id_t, struct window_s *> windows;
};
bool wayc_eventloop_init(struct eventloop_s *loop);
diff --git a/src/hashm.h b/src/hashm.h
index 67bb3e0..fb83391 100644
--- a/src/hashm.h
+++ b/src/hashm.h
@@ -8,40 +8,82 @@
#define WAYC_HASHMAP_SEED 0
#define WAYC_HASHMAP_CAP 16
-template <typename T> struct hashmap_s {
- struct hashmap *inner;
+template <typename K, typename V> struct hashentry_s {
+ K key;
+ V value;
};
-template <typename T>
-static inline i32 wayc_hashmap_hash(const void *item, uint64_t seed0,
- uint64_t seed1) {
+template <typename K, typename V>
+inline i32 wayc_hashentry_hash(const void *item, uint64_t seed0,
+ uint64_t seed1) {
wayc_notnull(item);
- return hashmap_xxhash3(item, sizeof(T), seed0, seed1);
+
+ const hashentry_s<K, V> *entry = (const hashentry_s<K, V> *)item;
+ return hashmap_murmur(&entry->key, sizeof(K), seed0, seed1);
}
-template <typename T>
-static inline bool wayc_hashmap_compare(const void *a, const void *b,
- void *udata) {
+template <typename K, typename V>
+inline bool wayc_hashentry_compare(const void *a, const void *b, void *udata) {
(void)udata;
wayc_notnull(a);
wayc_notnull(b);
- return memcmp(a, b, sizeof(T)) == 0;
+
+ const hashentry_s<K, V> *entry_a = (const hashentry_s<K, V> *)a;
+ const hashentry_s<K, V> *entry_b = (const hashentry_s<K, V> *)b;
+ return memcmp(&entry_a->key, &entry_b->key, sizeof(K)) == 0;
}
-template <typename T> inline hashmap_s<T> wayc_hashmap_init(void) {
- struct hashmap *map = hashmap_new_with_allocator(
- mi_malloc, mi_realloc, mi_free, sizeof(T), WAYC_HASHMAP_CAP,
- WAYC_HASHMAP_SEED, WAYC_HASHMAP_SEED, wayc_hashmap_hash<T>,
- wayc_hashmap_compare<T>, nullptr, nullptr);
+template <typename K, typename V> struct hashmap_s {
+ struct hashmap *inner;
+};
- hashmap_s<T> result;
- result.inner = map;
+template <typename K, typename V>
+inline void wayc_hashmap_init(hashmap_s<K, V> *map) {
+ wayc_notnull(map);
+ memset(map, 0, sizeof(*map));
- return result;
+ struct hashmap *inner;
+ inner = hashmap_new_with_allocator(
+ mi_malloc, mi_realloc, mi_free, sizeof(hashentry_s<K, V>),
+ WAYC_HASHMAP_CAP, WAYC_HASHMAP_SEED, WAYC_HASHMAP_SEED,
+ wayc_hashentry_hash<K, V>, wayc_hashentry_compare<K, V>, nullptr,
+ nullptr);
+
+ wayc_notnull(inner);
+
+ map->inner = inner;
}
-template <typename T> inline void wayc_hashmap_deinit(hashmap_s<T> *map) {
+template <typename K, typename V>
+inline void wayc_hashmap_deinit(hashmap_s<K, V> *map) {
+ wayc_notnull(map);
hashmap_free(map->inner);
- map->inner = nullptr;
-} \ No newline at end of file
+}
+
+template <typename K, typename V>
+inline V *wayc_hashmap_insert(hashmap_s<K, V> *map, const K *key) {
+ wayc_notnull(map);
+ wayc_notnull(key);
+
+ hashentry_s<K, V> entry{};
+ entry.key = *key;
+ auto *stored = (hashentry_s<K, V> *)hashmap_set(map->inner, &entry);
+
+ wayc_notnull(stored);
+ return &stored->value;
+}
+
+template <typename K, typename V>
+inline V *wayc_hashmap_get(hashmap_s<K, V> *map, const K *key) {
+ wayc_notnull(map);
+ wayc_notnull(key);
+
+ hashentry_s<K, V> entry{};
+ entry.key = *key;
+ auto *stored = (hashentry_s<K, V> *)hashmap_get(map->inner, &entry);
+ if (!stored)
+ return nullptr;
+
+ return &stored->value;
+}
diff --git a/src/window.h b/src/window.h
index 3b676f5..38a61c0 100644
--- a/src/window.h
+++ b/src/window.h
@@ -2,4 +2,6 @@
#include "utils.h"
+typedef u32 window_id_t;
+
struct window_s {}; \ No newline at end of file
diff --git a/src/wlstate.cc b/src/wlstate.cc
index 68846fb..da5e555 100644
--- a/src/wlstate.cc
+++ b/src/wlstate.cc
@@ -60,6 +60,7 @@ static struct wl_registry_listener WAYC_REGISTRY_LISTENER = {
bool 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)