From 22a45a63c1738e68c50eebe704fa7158f7a049cd Mon Sep 17 00:00:00 2001 From: Fabrice Date: Tue, 10 Feb 2026 10:32:20 +0100 Subject: eventloop update and looping --- src/events.cc | 18 ++++++++-- src/events.h | 5 ++- src/hash.h | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/hashm.h | 101 ---------------------------------------------------- src/wayclock.cc | 3 ++ 5 files changed, 130 insertions(+), 104 deletions(-) create mode 100644 src/hash.h delete mode 100644 src/hashm.h diff --git a/src/events.cc b/src/events.cc index edc47db..aed9de3 100644 --- a/src/events.cc +++ b/src/events.cc @@ -1,5 +1,5 @@ #include "events.h" -#include "hashm.h" +#include "hash.h" #include "vec.h" #include @@ -16,6 +16,7 @@ bool wayc_eventloop_init(struct eventloop_s *loop) { return false; loop->events = WAYC_VEC_INIT(struct event_s); + loop->running = true; wayc_hashmap_init(&loop->windows); return true; @@ -41,4 +42,17 @@ window_id_t wayc_eventloop_register(struct eventloop_s *loop, void wayc_eventloop_unregister(struct eventloop_s *loop, window_id_t winid) { wayc_notnull(loop); wayc_hashmap_remove(&loop->windows, &winid); -} \ No newline at end of file +} + +bool wayc_eventloop_running(struct eventloop_s *loop) { + wayc_notnull(loop); + return loop->running; +} + +void wayc_eventloop_upate(struct eventloop_s *loop) { + wayc_notnull(loop); + if (wayc_hashmap_count(&loop->windows) == 0) + loop->running = false; + + wayc_wlstate_update(&loop->state); +} diff --git a/src/events.h b/src/events.h index 0af78b3..a58b7a1 100644 --- a/src/events.h +++ b/src/events.h @@ -1,6 +1,6 @@ #pragma once -#include "hashm.h" +#include "hash.h" #include "vec.h" #include "window.h" #include "wlstate.h" @@ -19,6 +19,7 @@ struct eventloop_s { struct vec_s events; window_id_t winid; struct hashmap_s windows; + bool running; }; bool wayc_eventloop_init(struct eventloop_s *loop); @@ -26,3 +27,5 @@ void wayc_eventloop_deinit(struct eventloop_s *loop); window_id_t wayc_eventloop_register(struct eventloop_s *loop, struct window_s *window); void wayc_eventloop_unregister(struct eventloop_s *loop, window_id_t winid); +bool wayc_eventloop_running(struct eventloop_s *loop); +void wayc_eventloop_upate(struct eventloop_s *loop); diff --git a/src/hash.h b/src/hash.h new file mode 100644 index 0000000..66ed5ac --- /dev/null +++ b/src/hash.h @@ -0,0 +1,107 @@ +#pragma once + +#include "hashmap.h" +#include "utils.h" +#include +#include + +#define WAYC_HASHMAP_SEED 0 +#define WAYC_HASHMAP_CAP 16 + +template struct hashentry_s { + K key; + V value; +}; + +template +inline u64 wayc_hashentry_hash(const void *item, uint64_t seed0, + uint64_t seed1) { + wayc_notnull(item); + + const hashentry_s *entry = (const hashentry_s *)item; + return hashmap_murmur(&entry->key, sizeof(K), seed0, seed1); +} + +template +inline i32 wayc_hashentry_compare(const void *a, const void *b, void *udata) { + (void)udata; + + wayc_notnull(a); + wayc_notnull(b); + + const hashentry_s *entry_a = (const hashentry_s *)a; + const hashentry_s *entry_b = (const hashentry_s *)b; + return memcmp(&entry_a->key, &entry_b->key, sizeof(K)) == 0; +} + +template struct hashmap_s { + struct hashmap *inner; +}; + +template +inline void wayc_hashmap_init(hashmap_s *map) { + wayc_notnull(map); + memset(map, 0, sizeof(*map)); + + struct hashmap *inner; + inner = hashmap_new_with_allocator( + mi_malloc, mi_realloc, mi_free, sizeof(hashentry_s), + WAYC_HASHMAP_CAP, WAYC_HASHMAP_SEED, WAYC_HASHMAP_SEED, + wayc_hashentry_hash, wayc_hashentry_compare, nullptr, + nullptr); + + wayc_notnull(inner); + + map->inner = inner; +} + +template +inline void wayc_hashmap_deinit(hashmap_s *map) { + wayc_notnull(map); + hashmap_free(map->inner); +} + +template +inline V *wayc_hashmap_insert(hashmap_s *map, const K *key, + const V *value) { + wayc_notnull(map); + wayc_notnull(key); + wayc_notnull(value); + + hashentry_s entry{}; + entry.key = *key; + entry.value = *value; + hashentry_s *stored = + (hashentry_s *)hashmap_set(map->inner, &entry); + return &stored->value; +} + +template +inline V *wayc_hashmap_get(hashmap_s *map, const K *key) { + wayc_notnull(map); + wayc_notnull(key); + + hashentry_s entry{}; + entry.key = *key; + auto *stored = (hashentry_s *)hashmap_get(map->inner, &entry); + if (!stored) + return nullptr; + + return &stored->value; +} + +template +inline void wayc_hashmap_remove(hashmap_s *map, const K *key) { + wayc_notnull(map); + wayc_notnull(key); + + hashentry_s entry{}; + entry.key = *key; + hashmap_delete(map->inner, &entry); +} + +template +inline usize wayc_hashmap_count(hashmap_s *map) { + wayc_notnull(map); + return hashmap_count(map->inner); +} \ No newline at end of file diff --git a/src/hashm.h b/src/hashm.h deleted file mode 100644 index 16b025b..0000000 --- a/src/hashm.h +++ /dev/null @@ -1,101 +0,0 @@ -#pragma once - -#include "hashmap.h" -#include "utils.h" -#include -#include - -#define WAYC_HASHMAP_SEED 0 -#define WAYC_HASHMAP_CAP 16 - -template struct hashentry_s { - K key; - V value; -}; - -template -inline u64 wayc_hashentry_hash(const void *item, uint64_t seed0, - uint64_t seed1) { - wayc_notnull(item); - - const hashentry_s *entry = (const hashentry_s *)item; - return hashmap_murmur(&entry->key, sizeof(K), seed0, seed1); -} - -template -inline i32 wayc_hashentry_compare(const void *a, const void *b, void *udata) { - (void)udata; - - wayc_notnull(a); - wayc_notnull(b); - - const hashentry_s *entry_a = (const hashentry_s *)a; - const hashentry_s *entry_b = (const hashentry_s *)b; - return memcmp(&entry_a->key, &entry_b->key, sizeof(K)) == 0; -} - -template struct hashmap_s { - struct hashmap *inner; -}; - -template -inline void wayc_hashmap_init(hashmap_s *map) { - wayc_notnull(map); - memset(map, 0, sizeof(*map)); - - struct hashmap *inner; - inner = hashmap_new_with_allocator( - mi_malloc, mi_realloc, mi_free, sizeof(hashentry_s), - WAYC_HASHMAP_CAP, WAYC_HASHMAP_SEED, WAYC_HASHMAP_SEED, - wayc_hashentry_hash, wayc_hashentry_compare, nullptr, - nullptr); - - wayc_notnull(inner); - - map->inner = inner; -} - -template -inline void wayc_hashmap_deinit(hashmap_s *map) { - wayc_notnull(map); - hashmap_free(map->inner); -} - -template -inline V *wayc_hashmap_insert(hashmap_s *map, const K *key, - const V *value) { - wayc_notnull(map); - wayc_notnull(key); - wayc_notnull(value); - - hashentry_s entry{}; - entry.key = *key; - entry.value = *value; - hashentry_s *stored = - (hashentry_s *)hashmap_set(map->inner, &entry); - return &stored->value; -} - -template -inline V *wayc_hashmap_get(hashmap_s *map, const K *key) { - wayc_notnull(map); - wayc_notnull(key); - - hashentry_s entry{}; - entry.key = *key; - auto *stored = (hashentry_s *)hashmap_get(map->inner, &entry); - if (!stored) - return nullptr; - - return &stored->value; -} - -template -inline void wayc_hashmap_remove(hashmap_s *map, const K *key) { - wayc_notnull(map); - wayc_notnull(key); - - hashentry_s entry{}; - entry.key = *key; - hashmap_delete(map->inner, &entry); -} \ No newline at end of file diff --git a/src/wayclock.cc b/src/wayclock.cc index 6114cbe..63ca323 100644 --- a/src/wayclock.cc +++ b/src/wayclock.cc @@ -5,6 +5,9 @@ int main() { if (!wayc_eventloop_init(&loop)) wayc_panic("Failed to initialize event loop"); + while (wayc_eventloop_running(&loop)) + wayc_eventloop_upate(&loop); + wayc_eventloop_deinit(&loop); return 0; } \ No newline at end of file -- cgit v1.2.3