From c9fb356a095abb28602683cc3ca741aff481d671 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Mon, 23 Jan 2023 20:14:40 +0100 Subject: [PATCH] :art: load plugins at the start --- include/plug.h | 27 +++++++++++++++++++++++---- src/gwion.c | 3 ++- src/main.c | 5 ++++- src/plug.c | 24 +++++++++++++++--------- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/include/plug.h b/include/plug.h index 9b872f4c..1953d4e2 100644 --- a/include/plug.h +++ b/include/plug.h @@ -10,17 +10,36 @@ ANN void set_module(const struct Gwion_ *gwion, const m_str name, void *const ptr); ANN m_bool plugin_ini(struct Gwion_ *gwion, const m_str iname, const loc_t); +struct DriverData_; +struct Gwi_; +typedef m_bool (*gwplugin_t) (struct Gwi_*); +typedef void* (*gwmodini_t) (struct Gwion_ const* gwion, const Vector args); +typedef void* (*gwmodend_t) (struct Gwion_ const* gwion, void *self); +typedef void* (*gwdriver_t) (struct DriverData_ *); +typedef m_str* (*gwdepend_t) (void); + #define GWIMPORT_NAME import #define GWMODINI_NAME gwmodini #define GWMODEND_NAME gwmodend #define GWDRIVER_NAME gwdriver #define GWDEPEND_NAME gwdepend -#define GWMODINI(a) \ - ANN2(1) \ + +#ifndef GWION_BUILTIN +#define GWMODINI(a) \ + ANN2(1) \ void *GWMODINI_NAME(struct Gwion_ *const gwion NUSED, const Vector args NUSED) -#define GWMODEND(a) \ +#define GWMODEND(a) \ ANN void GWMODEND_NAME(struct Gwion_ *const gwion NUSED, void *self NUSED) #define GWDRIVER(a) ANN void GWDRIVER_NAME(DriverData *d) -#define GWDEPEND ANN m_str const *GWDEPEND_NAME(void) +#define GWDEPEND(a) ANN m_str const *GWDEPEND_NAME(void) +#else +#define GWIMODINI(a) \ + ANN2(1) \ + void *gwmodini_##a(struct Gwion_ *const gwion NUSED, const Vector args NUSED) +#define GWMODEND(a) \ + ANN void gwmodend_##a(struct Gwion_ *const gwion NUSED, void *self NUSED) +#define GWDRIVER(a) gwdriver_##a(a) ANN void GWDRIVER_NAME(DriverData *d) +#define GWDEPEND(a) gwdepend_##a ANN m_str const *GWDEPEND_NAME(void) +#endif #endif diff --git a/src/gwion.c b/src/gwion.c index 30db071f..7b11dedb 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -78,10 +78,11 @@ ANN static m_bool gwion_ok(const Gwion gwion, CliArg *arg) { if (gwion_audio(gwion) > 0) { CHECK_BB(plug_run(gwion, &arg->mod)); if (type_engine_init(gwion)) { + // embed_plug(gwion); vector_add(&gwion->data->plugs->vec, (m_uint)gwion->env->global_nspc); gwion->vm->cleaner_shred = gwion_cleaner(gwion); gwion->emit->locale = gwion_locale(gwion); - (void)arg_compile(gwion, arg); + // (void)arg_compile(gwion, arg); return GW_OK; } } diff --git a/src/main.c b/src/main.c index ccd94559..1184752c 100644 --- a/src/main.c +++ b/src/main.c @@ -52,9 +52,12 @@ int main(int argc, char **argv) { signal(SIGINT, sig); signal(SIGTERM, sig); const m_bool ini = gwion_ini(&gwion, &arg); +// embed_plug(&gwion); +gwion_embed(&gwion); + arg_compile(&gwion, &arg); arg_release(&arg); #ifdef GWION_EMBED_GW - embed_gw(&gwion); +// embed_gw(&gwion); #endif if (ini > 0) gwion_run(&gwion); gwion_end(&gwion); diff --git a/src/plug.c b/src/plug.c index 340ca71d..80260697 100644 --- a/src/plug.c +++ b/src/plug.c @@ -16,9 +16,6 @@ #include "gwi.h" typedef m_bool (*plugin)(Gwi); -typedef void *(*modini)(const struct Gwion_ *, const Vector); -typedef void *(*modend)(const struct Gwion_ *, void *); -typedef m_str *(*gwdeps)(void); struct PlugHandle { MemPool mp; @@ -30,11 +27,20 @@ typedef struct Plug_ { void *dl; void *self; Nspc nspc; + gwplugin_t plugin; + gwdriver_t driver; + gwmodini_t modini; + gwmodend_t modend; + gwdepend_t depend; } * Plug; ANN static struct Plug_ *new_plug(MemPool p, void *dl) { struct Plug_ *plug = mp_calloc(p, Plug); plug->dl = dl; + plug->plugin = DLSYM(plug->dl, gwplugin_t, GWIMPORT_NAME); + plug->modini = DLSYM(plug->dl, gwmodini_t, GWMODINI_NAME); + plug->modend = DLSYM(plug->dl, gwmodend_t, GWMODEND_NAME); + plug->depend = DLSYM(plug->dl, gwdepend_t, GWDEPEND_NAME); return plug; } @@ -99,7 +105,7 @@ void free_plug(const Gwion gwion) { const Map map = &gwion->data->plugs->map; for (m_uint i = 0; i < map_size(map); ++i) { const Plug plug = (Plug)VVAL(map, i); - const modend end = DLSYM(plug->dl, modend, GWMODEND_NAME); + const gwmodend_t end = plug->modend; if (end && plug->self) end(gwion, plug->self); free((m_str)VKEY(map, i)); DLCLOSE(plug->dl); @@ -139,8 +145,8 @@ ANN m_bool plug_run(const struct Gwion_ *gwion, const Map mod) { if (!strcmp(name, (m_str)VKEY(map, j))) { Plug plug = (Plug)VVAL(map, j); const Vector arg = opt ? split_args(gwion->mp, opt) : NULL; - const modini ini = DLSYM(plug->dl, modini, GWMODINI_NAME); - plug->self = ini(gwion, arg); + if(!plug->modini) continue; + plug->self = plug->modini(gwion, arg); if (arg) plug_free_arg(gwion->mp, arg); break; } @@ -154,7 +160,7 @@ ANN m_bool plug_run(const struct Gwion_ *gwion, const Map mod) { } ANN static m_bool dependencies(struct Gwion_ *gwion, const Plug plug, const loc_t loc) { - const gwdeps dep = DLSYM(plug->dl, gwdeps, GWDEPEND_NAME); + const gwdepend_t dep = plug->depend; bool ret = true; if (dep) { m_str *const base = dep(); @@ -183,7 +189,7 @@ ANN static void set_parent(const Nspc nspc, const Gwion gwion ) { } ANN static m_bool start(const Plug plug, const Gwion gwion, const m_str iname, const loc_t loc) { - const plugin imp = DLSYM(plug->dl, plugin, GWIMPORT_NAME); + if(!plug->plugin) return GW_ERROR; const bool cdoc = gwion->data->cdoc; gwion->data->cdoc = 0; // check cdoc CHECK_BB(dependencies(gwion, plug, loc)); @@ -194,7 +200,7 @@ ANN static m_bool start(const Plug plug, const Gwion gwion, const m_str iname, c const m_uint scope = env_push(gwion->env, NULL, plug->nspc); const m_str name = gwion->env->name; gwion->env->name = iname; - const m_bool ret = gwi_run(gwion, imp); + const m_bool ret = gwi_run(gwion, plug->plugin); gwion->env->name = name; env_pop(gwion->env, scope); return ret; -- 2.43.0