[backend,sdl] backend uses pub/sub to push events to main thread

This commit is contained in:
Milo Turner 2020-02-27 10:40:37 -05:00
parent 815fc64f8d
commit 7ff4794df7
5 changed files with 77 additions and 37 deletions

View File

@ -20,6 +20,7 @@ int ax__backend_new(
const char** out_err); const char** out_err);
struct msgq* ax__backend_msgq(struct ax_backend*); struct msgq* ax__backend_msgq(struct ax_backend*);
struct sub* ax__backend_sub_events(struct ax_backend*, struct rgn* dst_rgn);
bool ax__backend_is_shutdown(struct ax_backend*); bool ax__backend_is_shutdown(struct ax_backend*);
void ax__backend_wait_for_event(struct ax_backend*); void ax__backend_wait_for_event(struct ax_backend*);

View File

@ -4,8 +4,9 @@
#include "../util.h" #include "../util.h"
#include "../util/region.h" #include "../util/region.h"
#include "../util/functions.h" #include "../util/functions.h"
#include "../concurrent/msgq.h"
#include "../concurrent/msg.h" #include "../concurrent/msg.h"
#include "../concurrent/msgq.h"
#include "../concurrent/pubsub.h"
#include <unistd.h> #include <unistd.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
@ -22,6 +23,7 @@ size_t ax__backend_desired_region_size = MEDIUM;
struct ax_backend { struct ax_backend {
struct rgn* rgn; struct rgn* rgn;
struct msgq* inbox; struct msgq* inbox;
struct pubsub* events;
bool shutdown; bool shutdown;
struct window_list* windows; struct window_list* windows;
}; };
@ -52,6 +54,7 @@ int ax__backend_new(
bac->rgn = init_rgn; bac->rgn = init_rgn;
bac->inbox = msgq_new(init_rgn); bac->inbox = msgq_new(init_rgn);
bac->events = pubsub_new(init_rgn);
bac->shutdown = false; bac->shutdown = false;
bac->windows = NULL; bac->windows = NULL;
@ -80,6 +83,11 @@ struct msgq* ax__backend_msgq(struct ax_backend* bac)
return bac->inbox; return bac->inbox;
} }
struct sub* ax__backend_sub_events(struct ax_backend* bac, struct rgn* dst_rgn)
{
return sub_new(dst_rgn, bac->events);
}
bool ax__backend_is_shutdown(struct ax_backend* bac) bool ax__backend_is_shutdown(struct ax_backend* bac)
{ {
return bac->shutdown; return bac->shutdown;
@ -110,7 +118,7 @@ struct window_args_list {
static void load_fonts(struct ax_backend* bac, struct font_list* fl); static void load_fonts(struct ax_backend* bac, struct font_list* fl);
static void make_windows(struct ax_backend* bac, struct window_args_list* wl); static void make_windows(struct ax_backend* bac, struct window_args_list* wl);
static void handle_events(void); static void handle_events(struct ax_backend* bac);
static void render_windows(struct window_list* wl); static void render_windows(struct window_list* wl);
void ax__backend_step(struct ax_backend* bac) void ax__backend_step(struct ax_backend* bac)
@ -169,32 +177,12 @@ void ax__backend_step(struct ax_backend* bac)
load_fonts(bac, fonts); load_fonts(bac, fonts);
make_windows(bac, ax__reverse_list(windows)); make_windows(bac, ax__reverse_list(windows));
render_windows(bac->windows); render_windows(bac->windows);
handle_events(); handle_events(bac);
rgn_cleanup(&tmp_rgn); rgn_cleanup(&tmp_rgn);
// printf("ping. %zu message(s)\n", nmsg); // printf("ping. %zu message(s)\n", nmsg);
} }
/* -----------------------------------------------------------------------------
* Event handling
* -------------------------------------------------------------------------- */
static void handle_events(void)
{
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
case SDL_QUIT:
printf("pls quit...\n");
break;
default:
printf("some event of type: %d\n", ev.type);
break;
}
}
}
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* Fonts * Fonts
* -------------------------------------------------------------------------- */ * -------------------------------------------------------------------------- */
@ -320,3 +308,30 @@ static void render_windows(struct window_list* wl)
SDL_RenderPresent(r); SDL_RenderPresent(r);
} }
} }
/* -----------------------------------------------------------------------------
* Event handling
* -------------------------------------------------------------------------- */
static void handle_events(struct ax_backend* bac)
{
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
case SDL_QUIT:
for (struct window_list* wl = bac->windows;
wl != NULL;
wl = wl->next)
{
pubsub_begin_pub_typed(bac->events, ax_msg_window_closed)
->win = wl->win;
pubsub_end_pub(bac->events);
}
break;
default:
//printf("some event of type: %d\n", ev.type);
break;
}
}
}

View File

@ -1,4 +1,5 @@
[msg (ax_msg_shutdown_TAG "shutdown") [msg (ax_msg_shutdown_TAG "shutdown")
(ax_msg_load_font_TAG "load_font") (ax_msg_load_font_TAG "load_font")
(ax_msg_theme_loaded_TAG "theme_loaded") (ax_msg_theme_loaded_TAG "theme_loaded")
(ax_msg_make_window_TAG "make_window")] (ax_msg_make_window_TAG "make_window")
(ax_msg_window_closed_TAG "window_closed")]

View File

@ -11,6 +11,7 @@ enum {
ax_msg_load_font_TAG, ax_msg_load_font_TAG,
ax_msg_theme_loaded_TAG, ax_msg_theme_loaded_TAG,
ax_msg_make_window_TAG, ax_msg_make_window_TAG,
ax_msg_window_closed_TAG,
}; };
struct ax_msg_shutdown { struct ax_msg_shutdown {
@ -33,6 +34,10 @@ struct ax_msg_make_window {
int flags; int flags;
}; };
struct ax_msg_window_closed {
struct ax_window* win;
};
static inline const char* ax__msg_name(int val) static inline const char* ax__msg_name(int val)
{ {
#ifdef IMPOSSIBLE #ifdef IMPOSSIBLE

View File

@ -6,10 +6,20 @@
#include "../src/ctxt.h" #include "../src/ctxt.h"
#include "../src/backend.h" #include "../src/backend.h"
#include "../src/util/region.h"
#include "../src/concurrent/msg.h"
#include "../src/concurrent/pubsub.h"
struct rgn* rgn = NULL;
struct ax_ctxt* ax = NULL; struct ax_ctxt* ax = NULL;
static void cleanup() static void cleanup()
{ {
if (rgn != NULL) {
rgn_cleanup(rgn);
rgn = NULL;
}
ax_free(ax); ax_free(ax);
ax = NULL; ax = NULL;
} }
@ -45,6 +55,8 @@ int main(void)
ax = ax_new(); ax = ax_new();
ax_set_logger(ax, fileno(stdout), false); ax_set_logger(ax, fileno(stdout), false);
rgn = rgn_bootstrap_new(SMALL);
int rv = 0; int rv = 0;
#define GUARD(f) if ((rv = f) != 0) goto ax_fail; #define GUARD(f) if ((rv = f) != 0) goto ax_fail;
@ -56,20 +68,26 @@ int main(void)
GUARD(ax_select_theme(ax, thm)); GUARD(ax_select_theme(ax, thm));
GUARD(ax_select_window(ax, win)); GUARD(ax_select_window(ax, win));
usleep(1000 * 1000 * 2);
/* struct sub* ev_sub = ax__backend_sub_events(ax->bac, rgn);
struct ax_evt evt; for (bool quit = false; !quit; ) {
sub_begin_recv_and_wait(ev_sub);
SUB_RECV_ALL(ev_sub) {
for (;;) { ON(ax_msg_window_closed,
ax_wait_evt_avail(ax); {
while (ax_poll_evt(ax, &evt)) { if (m.win == win) {
if (evt.ty == AX_EVT_THEME_LOADED && evt.arg.theme == thm) { quit = true;
printf("The font finished loading!\n"); }
} break;
});
default:
printf("got a message: %s\n", ax__msg_name(m_type));
break;
} }
sub_end_recv(ev_sub);
} }
*/
cleanup(); cleanup();
return 0; return 0;