From 932e7fc708b266b5141f554837a06a7d3e5e32fd Mon Sep 17 00:00:00 2001 From: Milo Turner Date: Wed, 19 Feb 2020 12:15:46 -0500 Subject: [PATCH] [backend] design a little different --- src/backend.h | 24 ++++++++++++--- src/backend/sdl.c | 74 +++++++++++++++++++++++++++++------------------ 2 files changed, 66 insertions(+), 32 deletions(-) diff --git a/src/backend.h b/src/backend.h index 9fcdbcb..1e78fe6 100644 --- a/src/backend.h +++ b/src/backend.h @@ -2,9 +2,12 @@ #include #include +#include struct rgn; +struct msgq; struct ax_backend; +struct ax_theme; extern const char* ax__backend_impl_name; @@ -13,7 +16,20 @@ int ax__backend_new( struct ax_backend** out_bac, const char** out_err); -void ax__backend_enqueue_font( - struct ax_backend* bac, - const char* font_path, - size_t font_size); +enum { + ax_msg_shutdown_TAG, + ax_msg_load_font_TAG, +}; + +struct ax_msg_shutdown {}; + +struct ax_msg_load_font { + struct ax_theme* theme; + int category; +}; + +struct msgq* ax__backend_msgq(struct ax_backend*); + +void ax__backend_wait_for_event(struct ax_backend*); + +void ax__backend_step(struct ax_backend*, bool* out_shutdown); diff --git a/src/backend/sdl.c b/src/backend/sdl.c index d6eb406..e1851ef 100644 --- a/src/backend/sdl.c +++ b/src/backend/sdl.c @@ -1,32 +1,24 @@ #include "../backend.h" #include "../util/region.h" #include "../concurrent/msgq.h" +#include "../ctxt/theme.h" #include #include -/* --- Backend data type --- */ +/* ----------------------------------------------------------------------------- + * Backend type + * -------------------------------------------------------------------------- */ const char* ax__backend_impl_name = "SDL"; struct ax_backend { struct msgq* mq; + bool shutdown; }; -/* --- Async messages --- */ - -enum { - msg_close_TAG, - msg_load_font_TAG, -}; - -struct msg_close {}; - -struct msg_load_font { - const char* path; - size_t size; -}; - -/* --- Functions --- */ +/* ----------------------------------------------------------------------------- + * Backend functions + * -------------------------------------------------------------------------- */ static void backend_cleanup(struct ax_backend* bac); @@ -49,6 +41,7 @@ int ax__backend_new( rgn_pin(init_rgn, bac, (void*) backend_cleanup); bac->mq = msgq_new(init_rgn); + bac->shutdown = false; if (out_bac != NULL) { *out_bac = bac; @@ -64,21 +57,46 @@ fail: static void backend_cleanup(struct ax_backend* bac) { - msgq_begin_send_typed(bac->mq, msg_close); - msgq_end_send(bac->mq); - - // printf("bye, from SDL\n"); TTF_Quit(); SDL_Quit(); } -void ax__backend_enqueue_font( - struct ax_backend* bac, - const char* font_path, - size_t font_size) +struct msgq* ax__backend_msgq(struct ax_backend* bac) { - struct msg_load_font* m = msgq_begin_send_typed(bac->mq, msg_load_font); - m->path = rstrdup(&bac->mq->rgn, font_path); - m->size = font_size; - msgq_end_send(bac->mq); + return bac->mq; +} + +void ax__backend_wait_for_event(struct ax_backend* bac) +{ + (void) bac; +} + +void ax__backend_step(struct ax_backend* bac, bool* out_shutdown) +{ + int type; + void* data; + msgq_begin_recv(bac->mq); + while ((data = msgq_recv1(bac->mq, &type)) != NULL) { + switch (type) { + + case ax_msg_shutdown_TAG: + bac->shutdown = true; + break; + + case ax_msg_load_font_TAG: { + struct ax_msg_load_font* m = data; + printf("TODO: load font `%s' (size %zu)\n", + m->theme->font_path[m->category], + m->theme->font_size[m->category]); + break; + } + + default: break; + } + } + msgq_end_recv(bac->mq); + + if (out_shutdown != NULL) { + *out_shutdown = bac->shutdown; + } }