[theme] use pubsub for notifying when a theme is loaded

This commit is contained in:
Milo Turner 2020-02-25 14:55:57 -05:00
parent 8716a9c088
commit 815fc64f8d
3 changed files with 27 additions and 35 deletions

View File

@ -2,6 +2,7 @@
#include "../backend.h" #include "../backend.h"
#include "../concurrent/msg.h" #include "../concurrent/msg.h"
#include "../concurrent/msgq.h" #include "../concurrent/msgq.h"
#include "../concurrent/pubsub.h"
#include "../ctxt.h" #include "../ctxt.h"
#include "../util.h" #include "../util.h"
#include "theme.h" #include "theme.h"
@ -157,8 +158,7 @@ void ax_set_theme_font(
} }
} }
void ax_set_theme_iconset( void ax_set_theme_iconset(struct ax_ctxt* ax, const char* iconset_dir)
struct ax_ctxt* ax, const char* iconset_dir)
{ {
if (ax->thmb == NULL) { if (ax->thmb == NULL) {
ax_log(ax, "`ax_set_theme_iconset' called while not building a theme"); ax_log(ax, "`ax_set_theme_iconset' called while not building a theme");
@ -169,17 +169,17 @@ void ax_set_theme_iconset(
void ax_theme_wait_until_loaded(struct ax_ctxt* ax, struct ax_theme* thm) void ax_theme_wait_until_loaded(struct ax_ctxt* ax, struct ax_theme* thm)
{ {
struct rgn rgn[1]; struct rgn rgn;
rgn_init(rgn, SMALL); rgn_init(&rgn, SMALL);
struct msgq* mq = msgq_new(rgn); struct sub* sub = ax__theme_sub_loaded(thm, &rgn);
ax__theme_on_load(thm, mq); if (sub != NULL) {
msgq_begin_recv_and_wait(mq); sub_begin_recv_and_wait(sub);
MSGQ_RECV_ALL(mq) { SUB_RECV_ALL(sub) { default: break; }
default: sub_end_recv(sub);
break; } else {
ax_log(ax, "subscriber is NULL, this means the theme is already loaded");
} }
msgq_end_recv(mq); rgn_cleanup(&rgn);
rgn_cleanup(rgn);
} }
int ax_select_theme(struct ax_ctxt* ax, struct ax_theme* thm) int ax_select_theme(struct ax_ctxt* ax, struct ax_theme* thm)

View File

@ -4,6 +4,7 @@
#include "../backend.h" #include "../backend.h"
#include "../concurrent/msgq.h" #include "../concurrent/msgq.h"
#include "../concurrent/msg.h" #include "../concurrent/msg.h"
#include "../concurrent/pubsub.h"
#include <inttypes.h> #include <inttypes.h>
static const char* default_font_path(struct rgn* rgn) static const char* default_font_path(struct rgn* rgn)
@ -98,7 +99,7 @@ struct ax_theme* ax__theme_builder_finish(
thm->font_handle[i] = NULL; thm->font_handle[i] = NULL;
} }
thm->on_load_rgn = rgn_new(dst_rgn, SMALL); thm->on_load_rgn = rgn_new(dst_rgn, SMALL);
thm->on_load = NULL; thm->on_load = pubsub_new(dst_rgn);
pthread_mutex_init(&thm->mx, NULL); pthread_mutex_init(&thm->mx, NULL);
rgn_pin(dst_rgn, thm, (void*) pthread_mutex_destroy); rgn_pin(dst_rgn, thm, (void*) pthread_mutex_destroy);
@ -127,13 +128,6 @@ static inline bool is_loaded(struct ax_theme* thm)
return true; return true;
} }
static inline void msgq_send_theme_loaded(struct msgq* mq, struct ax_theme* thm)
{
msgq_begin_send_typed(mq, ax_msg_theme_loaded)
->theme = thm;
msgq_end_send(mq);
}
struct ax_font_h* ax__theme_font_handle( struct ax_font_h* ax__theme_font_handle(
struct ax_theme* thm, struct ax_theme* thm,
enum ax_font_cat i) enum ax_font_cat i)
@ -160,27 +154,24 @@ void ax__theme_set_font_handle(
pthread_mutex_lock(&thm->mx); pthread_mutex_lock(&thm->mx);
thm->font_handle[i] = h; thm->font_handle[i] = h;
if (is_loaded(thm)) { if (is_loaded(thm)) {
while (thm->on_load != NULL) { pubsub_begin_pub_typed(thm->on_load, ax_msg_theme_loaded)
msgq_send_theme_loaded(thm->on_load->mq, thm); ->theme = thm;
thm->on_load = thm->on_load->next; pubsub_end_pub(thm->on_load);
}
} }
pthread_mutex_unlock(&thm->mx); pthread_mutex_unlock(&thm->mx);
} }
void ax__theme_on_load(struct ax_theme* thm, struct msgq* res_mq) struct sub* ax__theme_sub_loaded(struct ax_theme* thm, struct rgn* dst_rgn)
{ {
struct sub* sub;
pthread_mutex_lock(&thm->mx); pthread_mutex_lock(&thm->mx);
if (is_loaded(thm)) { if (is_loaded(thm)) {
msgq_send_theme_loaded(res_mq, thm); sub = NULL;
} else { } else {
struct msgq_list* l = sub = sub_new(dst_rgn, thm->on_load);
ralloc_typed(thm->on_load_rgn, struct msgq_list, 1);
l->mq = res_mq;
l->next = thm->on_load;
thm->on_load = l;
} }
pthread_mutex_unlock(&thm->mx); pthread_mutex_unlock(&thm->mx);
return sub;
} }
int ax__string_to_color_cat(const char* str, enum ax_color_cat* out_cat) int ax__string_to_color_cat(const char* str, enum ax_color_cat* out_cat)

View File

@ -7,7 +7,8 @@
struct rgn; struct rgn;
struct msgq; struct msgq;
struct msgq_list; struct pubsub;
struct sub;
struct ax_font_h; struct ax_font_h;
#define THEME_BUILDER_DESIRED_REGION_SIZE (16*1024) #define THEME_BUILDER_DESIRED_REGION_SIZE (16*1024)
@ -44,7 +45,7 @@ struct ax_theme {
const char* font_path[AX_FONT__COUNT]; const char* font_path[AX_FONT__COUNT];
struct ax_font_h* font_handle[AX_FONT__COUNT]; struct ax_font_h* font_handle[AX_FONT__COUNT];
struct rgn* on_load_rgn; struct rgn* on_load_rgn;
struct msgq_list* on_load; struct pubsub* on_load;
pthread_mutex_t mx; pthread_mutex_t mx;
}; };
@ -107,9 +108,9 @@ void ax__theme_set_font_handle(
enum ax_font_cat i, enum ax_font_cat i,
struct ax_font_h* h); struct ax_font_h* h);
void ax__theme_on_load( struct sub* ax__theme_sub_loaded(
struct ax_theme* thm, struct ax_theme* thm,
struct msgq* res_mq); struct rgn* dst_rgn);
/* /*
void ax__debug_theme(struct ax_theme* thm); void ax__debug_theme(struct ax_theme* thm);