diff --git a/src/backend.h b/src/backend.h index 1e78fe6..fe16a89 100644 --- a/src/backend.h +++ b/src/backend.h @@ -16,20 +16,8 @@ int ax__backend_new( struct ax_backend** out_bac, const char** out_err); -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*); +bool ax__backend_is_shutdown(struct ax_backend*); void ax__backend_wait_for_event(struct ax_backend*); - -void ax__backend_step(struct ax_backend*, bool* out_shutdown); +void ax__backend_step(struct ax_backend*); diff --git a/src/backend/sdl.c b/src/backend/sdl.c index e1851ef..171f6ff 100644 --- a/src/backend/sdl.c +++ b/src/backend/sdl.c @@ -1,7 +1,8 @@ #include "../backend.h" +#include "../ctxt/theme.h" #include "../util/region.h" #include "../concurrent/msgq.h" -#include "../ctxt/theme.h" +#include "../concurrent/msg.h" #include #include @@ -66,37 +67,39 @@ struct msgq* ax__backend_msgq(struct ax_backend* bac) return bac->mq; } +bool ax__backend_is_shutdown(struct ax_backend* bac) +{ + return bac->shutdown; +} + void ax__backend_wait_for_event(struct ax_backend* bac) { (void) bac; } -void ax__backend_step(struct ax_backend* bac, bool* out_shutdown) +void ax__backend_step(struct ax_backend* bac) { - 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; + MSGQ_RECV_ALL(bac->mq) { - 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; - } + ON(ax_msg_shutdown, + { + (void) m; + bac->shutdown = true; + break; + }); + + ON(ax_msg_load_font, + { + 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; - } + msgq_end_recv(bac->mq); } diff --git a/src/concurrent/msg.h b/src/concurrent/msg.h new file mode 100644 index 0000000..6c45d3c --- /dev/null +++ b/src/concurrent/msg.h @@ -0,0 +1,14 @@ +#pragma once + +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; +}; diff --git a/src/concurrent/msgq.h b/src/concurrent/msgq.h index 3acdc8d..633f8e2 100644 --- a/src/concurrent/msgq.h +++ b/src/concurrent/msgq.h @@ -21,9 +21,22 @@ struct msgq* msgq_new(struct rgn* init_rgn); void* msgq_begin_send(struct msgq* mq, int type, size_t payload_size); void msgq_end_send(struct msgq* mq); -#define msgq_begin_send_typed(_mq, _ty) \ - ((struct _ty*) msgq_begin_send(_mq, _ty ## _TAG, sizeof(struct _ty))) - void msgq_begin_recv(struct msgq* mq); void* msgq_recv1(struct msgq* mq, int* out_type); void msgq_end_recv(struct msgq* mq); + +// helper macros + +#define msgq_begin_send_typed(_mq, ty) \ + ((struct ty*) msgq_begin_send(_mq, ty ## _TAG, sizeof(struct ty))) + +#define MSGQ_RECV_ALL(_mq) \ + int mqr__type; void* mqr__data; \ + while ((mqr__data = msgq_recv1(_mq, &mqr__type)) != NULL) \ + switch (mqr__type) + +#define ON(ty, _body) \ + case ty ## _TAG: { \ + const struct ty* m = mqr__data; \ + _body; \ + } diff --git a/src/ctxt/theme.c b/src/ctxt/theme.c index ca93c6f..3250346 100644 --- a/src/ctxt/theme.c +++ b/src/ctxt/theme.c @@ -3,6 +3,7 @@ #include "../util/region.h" #include "../backend.h" #include "../concurrent/msgq.h" +#include "../concurrent/msg.h" #include static const char* default_font_path(struct rgn* rgn) diff --git a/test/main.c b/test/main.c index 1961e58..0889cc9 100644 --- a/test/main.c +++ b/test/main.c @@ -22,9 +22,8 @@ int main(void) ax_log(ax, "Got here\n"); - bool shutdown = false; ax__backend_wait_for_event(ax->bac); - ax__backend_step(ax->bac, &shutdown); + ax__backend_step(ax->bac); //cleanup: if (rv != 0) {