[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 "../concurrent/msg.h"
#include "../concurrent/msgq.h"
#include "../concurrent/pubsub.h"
#include "../ctxt.h"
#include "../util.h"
#include "theme.h"
@ -157,8 +158,7 @@ void ax_set_theme_font(
}
}
void ax_set_theme_iconset(
struct ax_ctxt* ax, const char* iconset_dir)
void ax_set_theme_iconset(struct ax_ctxt* ax, const char* iconset_dir)
{
if (ax->thmb == NULL) {
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)
{
struct rgn rgn[1];
rgn_init(rgn, SMALL);
struct msgq* mq = msgq_new(rgn);
ax__theme_on_load(thm, mq);
msgq_begin_recv_and_wait(mq);
MSGQ_RECV_ALL(mq) {
default:
break;
struct rgn rgn;
rgn_init(&rgn, SMALL);
struct sub* sub = ax__theme_sub_loaded(thm, &rgn);
if (sub != NULL) {
sub_begin_recv_and_wait(sub);
SUB_RECV_ALL(sub) { default: break; }
sub_end_recv(sub);
} 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)

View File

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

View File

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