modules: get rid of struct module_info

Since this struct only contained function pointers, make all modules
export those functions directly.

The plugin manager now defines a module interface struct, and fills it
it by dlsym:ing the functions that used to be in module_info.
This commit is contained in:
Daniel Eklöf 2019-01-13 17:09:11 +01:00
parent 07b1615a41
commit bc62843c91
16 changed files with 57 additions and 111 deletions

View file

@ -335,17 +335,17 @@ verify_module(keychain_t *chain, const struct yml_node *node)
return false; return false;
} }
const struct module_info *info = plugin_load_module(mod_name); const struct module_iface *iface = plugin_load_module(mod_name);
if (info == NULL) { if (iface == NULL) {
LOG_ERR( LOG_ERR(
"%s: invalid module name: %s", conf_err_prefix(chain, node), mod_name); "%s: invalid module name: %s", conf_err_prefix(chain, node), mod_name);
return false; return false;
} }
assert(info->verify_conf != NULL); assert(iface->verify_conf != NULL);
chain_push(chain, mod_name); chain_push(chain, mod_name);
if (!info->verify_conf(chain, values)) if (!iface->verify_conf(chain, values))
return false; return false;
chain_pop(chain); chain_pop(chain);

View file

@ -340,8 +340,8 @@ conf_to_bar(const struct yml_node *bar)
? conf_to_color(mod_foreground) : inherited.foreground, ? conf_to_color(mod_foreground) : inherited.foreground,
}; };
const struct module_info *info = plugin_load_module(mod_name); const struct module_iface *iface = plugin_load_module(mod_name);
mods[idx] = info->from_conf(m.value, mod_inherit); mods[idx] = iface->from_conf(m.value, mod_inherit);
} }
if (i == 0) { if (i == 0) {

View file

@ -9,18 +9,6 @@
#include "yml.h" #include "yml.h"
struct bar; struct bar;
struct module;
struct module_info {
bool (*verify_conf)(keychain_t *chain, const struct yml_node *node);
struct module *(*from_conf)(
const struct yml_node *node, struct conf_inherit inherited);
#define MODULE_COMMON_ATTRS \
{"font", false, &conf_verify_font}, \
{"foreground", false, &conf_verify_color}, \
{NULL, false, NULL}
};
struct module { struct module {
const struct bar *bar; const struct bar *bar;
@ -47,3 +35,9 @@ struct module {
struct module *module_common_new(void); struct module *module_common_new(void);
void module_default_destroy(struct module *mod); void module_default_destroy(struct module *mod);
struct exposable *module_begin_expose(struct module *mod); struct exposable *module_begin_expose(struct module *mod);
/* List of attributes *all* modules implement */
#define MODULE_COMMON_ATTRS \
{"font", false, &conf_verify_font}, \
{"foreground", false, &conf_verify_color}, \
{NULL, false, NULL}

View file

@ -266,7 +266,7 @@ alsa_new(const char *card, const char *mixer, struct particle *label)
return mod; return mod;
} }
static struct module * struct module *
from_conf(const struct yml_node *node, struct conf_inherit inherited) from_conf(const struct yml_node *node, struct conf_inherit inherited)
{ {
const struct yml_node *card = yml_get_value(node, "card"); const struct yml_node *card = yml_get_value(node, "card");
@ -279,7 +279,7 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited)
conf_to_particle(content, inherited)); conf_to_particle(content, inherited));
} }
static bool bool
verify_conf(keychain_t *chain, const struct yml_node *node) verify_conf(keychain_t *chain, const struct yml_node *node)
{ {
static const struct attr_info attrs[] = { static const struct attr_info attrs[] = {
@ -292,8 +292,3 @@ verify_conf(keychain_t *chain, const struct yml_node *node)
return conf_verify_dict(chain, node, attrs); return conf_verify_dict(chain, node, attrs);
} }
const struct module_info plugin_info = {
.verify_conf = &verify_conf,
.from_conf = &from_conf,
};

View file

@ -214,7 +214,7 @@ backlight_new(const char *device, struct particle *label)
return mod; return mod;
} }
static struct module * struct module *
from_conf(const struct yml_node *node, struct conf_inherit inherited) from_conf(const struct yml_node *node, struct conf_inherit inherited)
{ {
const struct yml_node *name = yml_get_value(node, "name"); const struct yml_node *name = yml_get_value(node, "name");
@ -224,7 +224,7 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited)
yml_value_as_string(name), conf_to_particle(c, inherited)); yml_value_as_string(name), conf_to_particle(c, inherited));
} }
static bool bool
verify_conf(keychain_t *chain, const struct yml_node *node) verify_conf(keychain_t *chain, const struct yml_node *node)
{ {
static const struct attr_info attrs[] = { static const struct attr_info attrs[] = {
@ -236,8 +236,3 @@ verify_conf(keychain_t *chain, const struct yml_node *node)
return conf_verify_dict(chain, node, attrs); return conf_verify_dict(chain, node, attrs);
} }
const struct module_info plugin_info = {
.verify_conf = &verify_conf,
.from_conf = &from_conf,
};

View file

@ -348,7 +348,7 @@ battery_new(const char *battery, struct particle *label, int poll_interval_secs)
return mod; return mod;
} }
static struct module * struct module *
from_conf(const struct yml_node *node, struct conf_inherit inherited) from_conf(const struct yml_node *node, struct conf_inherit inherited)
{ {
const struct yml_node *c = yml_get_value(node, "content"); const struct yml_node *c = yml_get_value(node, "content");
@ -361,7 +361,7 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited)
poll_interval != NULL ? yml_value_as_int(poll_interval) : 60); poll_interval != NULL ? yml_value_as_int(poll_interval) : 60);
} }
static bool bool
verify_conf(keychain_t *chain, const struct yml_node *node) verify_conf(keychain_t *chain, const struct yml_node *node)
{ {
static const struct attr_info attrs[] = { static const struct attr_info attrs[] = {
@ -374,8 +374,3 @@ verify_conf(keychain_t *chain, const struct yml_node *node)
return conf_verify_dict(chain, node, attrs); return conf_verify_dict(chain, node, attrs);
} }
const struct module_info plugin_info = {
.verify_conf = &verify_conf,
.from_conf = &from_conf,
};

View file

@ -93,7 +93,7 @@ clock_new(struct particle *label, const char *date_format, const char *time_form
return mod; return mod;
} }
static struct module * struct module *
from_conf(const struct yml_node *node, struct conf_inherit inherited) from_conf(const struct yml_node *node, struct conf_inherit inherited)
{ {
const struct yml_node *c = yml_get_value(node, "content"); const struct yml_node *c = yml_get_value(node, "content");
@ -106,7 +106,7 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited)
time_format != NULL ? yml_value_as_string(time_format) : "%H:%M"); time_format != NULL ? yml_value_as_string(time_format) : "%H:%M");
} }
static bool bool
verify_conf(keychain_t *chain, const struct yml_node *node) verify_conf(keychain_t *chain, const struct yml_node *node)
{ {
static const struct attr_info attrs[] = { static const struct attr_info attrs[] = {
@ -119,8 +119,3 @@ verify_conf(keychain_t *chain, const struct yml_node *node)
return conf_verify_dict(chain, node, attrs); return conf_verify_dict(chain, node, attrs);
} }
const struct module_info plugin_info = {
.verify_conf = &verify_conf,
.from_conf = &from_conf,
};

View file

@ -639,7 +639,7 @@ i3_new(struct i3_workspaces workspaces[], size_t workspace_count,
return mod; return mod;
} }
static struct module * struct module *
from_conf(const struct yml_node *node, struct conf_inherit inherited) from_conf(const struct yml_node *node, struct conf_inherit inherited)
{ {
const struct yml_node *c = yml_get_value(node, "content"); const struct yml_node *c = yml_get_value(node, "content");
@ -696,7 +696,7 @@ verify_content(keychain_t *chain, const struct yml_node *node)
return true; return true;
} }
static bool bool
verify_conf(keychain_t *chain, const struct yml_node *node) verify_conf(keychain_t *chain, const struct yml_node *node)
{ {
static const struct attr_info attrs[] = { static const struct attr_info attrs[] = {
@ -710,8 +710,3 @@ verify_conf(keychain_t *chain, const struct yml_node *node)
return conf_verify_dict(chain, node, attrs); return conf_verify_dict(chain, node, attrs);
} }
const struct module_info plugin_info = {
.verify_conf = &verify_conf,
.from_conf = &from_conf,
};

View file

@ -46,14 +46,14 @@ label_new(struct particle *label)
return mod; return mod;
} }
static struct module * struct module *
from_conf(const struct yml_node *node, struct conf_inherit inherited) from_conf(const struct yml_node *node, struct conf_inherit inherited)
{ {
const struct yml_node *c = yml_get_value(node, "content"); const struct yml_node *c = yml_get_value(node, "content");
return label_new(conf_to_particle(c, inherited)); return label_new(conf_to_particle(c, inherited));
} }
static bool bool
verify_conf(keychain_t *chain, const struct yml_node *node) verify_conf(keychain_t *chain, const struct yml_node *node)
{ {
static const struct attr_info attrs[] = { static const struct attr_info attrs[] = {
@ -64,8 +64,3 @@ verify_conf(keychain_t *chain, const struct yml_node *node)
return conf_verify_dict(chain, node, attrs); return conf_verify_dict(chain, node, attrs);
} }
const struct module_info plugin_info = {
.verify_conf = &verify_conf,
.from_conf = &from_conf,
};

View file

@ -475,7 +475,7 @@ mpd_new(const char *host, uint16_t port, struct particle *label)
return mod; return mod;
} }
static struct module * struct module *
from_conf(const struct yml_node *node, struct conf_inherit inherited) from_conf(const struct yml_node *node, struct conf_inherit inherited)
{ {
const struct yml_node *host = yml_get_value(node, "host"); const struct yml_node *host = yml_get_value(node, "host");
@ -488,7 +488,7 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited)
conf_to_particle(c, inherited)); conf_to_particle(c, inherited));
} }
static bool bool
verify_conf(keychain_t *chain, const struct yml_node *node) verify_conf(keychain_t *chain, const struct yml_node *node)
{ {
static const struct attr_info attrs[] = { static const struct attr_info attrs[] = {
@ -501,8 +501,3 @@ verify_conf(keychain_t *chain, const struct yml_node *node)
return conf_verify_dict(chain, node, attrs); return conf_verify_dict(chain, node, attrs);
} }
const struct module_info plugin_info = {
.verify_conf = &verify_conf,
.from_conf = &from_conf,
};

View file

@ -530,7 +530,7 @@ network_new(const char *iface, struct particle *label)
return mod; return mod;
} }
static struct module * struct module *
from_conf(const struct yml_node *node, struct conf_inherit inherited) from_conf(const struct yml_node *node, struct conf_inherit inherited)
{ {
const struct yml_node *name = yml_get_value(node, "name"); const struct yml_node *name = yml_get_value(node, "name");
@ -540,7 +540,7 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited)
yml_value_as_string(name), conf_to_particle(content, inherited)); yml_value_as_string(name), conf_to_particle(content, inherited));
} }
static bool bool
verify_conf(keychain_t *chain, const struct yml_node *node) verify_conf(keychain_t *chain, const struct yml_node *node)
{ {
static const struct attr_info attrs[] = { static const struct attr_info attrs[] = {
@ -552,8 +552,3 @@ verify_conf(keychain_t *chain, const struct yml_node *node)
return conf_verify_dict(chain, node, attrs); return conf_verify_dict(chain, node, attrs);
} }
const struct module_info plugin_info = {
.verify_conf = &verify_conf,
.from_conf = &from_conf,
};

View file

@ -557,7 +557,7 @@ removables_new(struct particle *label, int left_spacing, int right_spacing)
return mod; return mod;
} }
static struct module * struct module *
from_conf(const struct yml_node *node, struct conf_inherit inherited) from_conf(const struct yml_node *node, struct conf_inherit inherited)
{ {
const struct yml_node *content = yml_get_value(node, "content"); const struct yml_node *content = yml_get_value(node, "content");
@ -573,7 +573,7 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited)
return removables_new(conf_to_particle(content, inherited), left, right); return removables_new(conf_to_particle(content, inherited), left, right);
} }
static bool bool
verify_conf(keychain_t *chain, const struct yml_node *node) verify_conf(keychain_t *chain, const struct yml_node *node)
{ {
static const struct attr_info attrs[] = { static const struct attr_info attrs[] = {
@ -587,8 +587,3 @@ verify_conf(keychain_t *chain, const struct yml_node *node)
return conf_verify_dict(chain, node, attrs); return conf_verify_dict(chain, node, attrs);
} }
const struct module_info plugin_info = {
.verify_conf = &verify_conf,
.from_conf = &from_conf,
};

View file

@ -467,14 +467,14 @@ xkb_new(struct particle *label)
return mod; return mod;
} }
static struct module * struct module *
from_conf(const struct yml_node *node, struct conf_inherit inherited) from_conf(const struct yml_node *node, struct conf_inherit inherited)
{ {
const struct yml_node *c = yml_get_value(node, "content"); const struct yml_node *c = yml_get_value(node, "content");
return xkb_new(conf_to_particle(c, inherited)); return xkb_new(conf_to_particle(c, inherited));
} }
static bool bool
verify_conf(keychain_t *chain, const struct yml_node *node) verify_conf(keychain_t *chain, const struct yml_node *node)
{ {
static const struct attr_info attrs[] = { static const struct attr_info attrs[] = {
@ -485,8 +485,3 @@ verify_conf(keychain_t *chain, const struct yml_node *node)
return conf_verify_dict(chain, node, attrs); return conf_verify_dict(chain, node, attrs);
} }
const struct module_info plugin_info = {
.verify_conf = &verify_conf,
.from_conf = &from_conf,
};

View file

@ -311,14 +311,14 @@ xwindow_new(struct particle *label)
return mod; return mod;
} }
static struct module * struct module *
from_conf(const struct yml_node *node, struct conf_inherit inherited) from_conf(const struct yml_node *node, struct conf_inherit inherited)
{ {
const struct yml_node *c = yml_get_value(node, "content"); const struct yml_node *c = yml_get_value(node, "content");
return xwindow_new(conf_to_particle(c, inherited)); return xwindow_new(conf_to_particle(c, inherited));
} }
static bool bool
verify_conf(keychain_t *chain, const struct yml_node *node) verify_conf(keychain_t *chain, const struct yml_node *node)
{ {
static const struct attr_info attrs[] = { static const struct attr_info attrs[] = {
@ -329,8 +329,3 @@ verify_conf(keychain_t *chain, const struct yml_node *node)
return conf_verify_dict(chain, node, attrs); return conf_verify_dict(chain, node, attrs);
} }
const struct module_info plugin_info = {
.verify_conf = &verify_conf,
.from_conf = &from_conf,
};

View file

@ -47,7 +47,8 @@ plugin_load(const char *name, enum plugin_type type)
tll_foreach(plugins, plug) { tll_foreach(plugins, plug) {
if (plug->item.type == type && strcmp(plug->item.name, name) == 0) { if (plug->item.type == type && strcmp(plug->item.name, name) == 0) {
LOG_DBG("%s: %s already loaded: %p", type2str(type), name, plug->item.lib); LOG_DBG("%s: %s already loaded: %p", type2str(type), name, plug->item.lib);
assert(plug->item.sym != NULL); assert(plug->item.dummy.sym1 != NULL);
assert(plug->item.dummy.sym2 != NULL);
return &plug->item; return &plug->item;
} }
} }
@ -66,23 +67,18 @@ plugin_load(const char *name, enum plugin_type type)
return NULL; return NULL;
} }
tll_push_back(plugins, ((struct plugin){strdup(name), type, lib, {NULL}})); tll_push_back(plugins, ((struct plugin){strdup(name), type, lib, {{NULL}}}));
struct plugin *plug = &tll_back(plugins); struct plugin *plug = &tll_back(plugins);
dlerror(); /* Clear previous error */ dlerror(); /* Clear previous error */
const char *dl_error = NULL; const char *dl_error = NULL;
if (type == PLUGIN_MODULE) { plug->dummy.sym1 = dlsym(lib, "verify_conf");
plug->sym = dlsym(lib, "plugin_info"); dl_error = dlerror();
dl_error = dlerror();
} else {
plug->particle.verify_conf = dlsym(lib, "verify_conf");
dl_error = dlerror();
if (dl_error == NULL) { if (dl_error == NULL) {
plug->particle.from_conf = dlsym(lib, "from_conf"); plug->dummy.sym2 = dlsym(lib, "from_conf");
dl_error = dlerror(); dl_error = dlerror();
}
} }
if (dl_error != NULL) { if (dl_error != NULL) {
@ -93,11 +89,11 @@ plugin_load(const char *name, enum plugin_type type)
return plug; return plug;
} }
const struct module_info * const struct module_iface *
plugin_load_module(const char *name) plugin_load_module(const char *name)
{ {
const struct plugin *plug = plugin_load(name, PLUGIN_MODULE); const struct plugin *plug = plugin_load(name, PLUGIN_MODULE);
return plug != NULL ? plug->sym : NULL; return plug != NULL ? &plug->module : NULL;
} }
const struct particle_iface * const struct particle_iface *

View file

@ -4,13 +4,19 @@
#include "module.h" #include "module.h"
#include "particle.h" #include "particle.h"
struct module_iface {
bool (*verify_conf)(keychain_t *chain, const struct yml_node *node);
struct module *(*from_conf)(
const struct yml_node *node, struct conf_inherit inherited);
};
struct particle_iface { struct particle_iface {
bool (*verify_conf)(keychain_t *chain, const struct yml_node *node); bool (*verify_conf)(keychain_t *chain, const struct yml_node *node);
struct particle *(*from_conf)( struct particle *(*from_conf)(
const struct yml_node *node, struct particle *common); const struct yml_node *node, struct particle *common);
}; };
const struct module_info *plugin_load_module(const char *name); const struct module_iface *plugin_load_module(const char *name);
const struct particle_iface *plugin_load_particle(const char *name); const struct particle_iface *plugin_load_particle(const char *name);
enum plugin_type { PLUGIN_MODULE, PLUGIN_PARTICLE }; enum plugin_type { PLUGIN_MODULE, PLUGIN_PARTICLE };
@ -21,7 +27,12 @@ struct plugin {
void *lib; void *lib;
union { union {
void *sym; struct {
void *sym1;
void *sym2;
} dummy;
struct module_iface module;
struct particle_iface particle; struct particle_iface particle;
}; };
}; };