forked from external/yambar
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:
parent
07b1615a41
commit
bc62843c91
16 changed files with 57 additions and 111 deletions
|
@ -335,17 +335,17 @@ verify_module(keychain_t *chain, const struct yml_node *node)
|
|||
return false;
|
||||
}
|
||||
|
||||
const struct module_info *info = plugin_load_module(mod_name);
|
||||
if (info == NULL) {
|
||||
const struct module_iface *iface = plugin_load_module(mod_name);
|
||||
if (iface == NULL) {
|
||||
LOG_ERR(
|
||||
"%s: invalid module name: %s", conf_err_prefix(chain, node), mod_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
assert(info->verify_conf != NULL);
|
||||
assert(iface->verify_conf != NULL);
|
||||
|
||||
chain_push(chain, mod_name);
|
||||
if (!info->verify_conf(chain, values))
|
||||
if (!iface->verify_conf(chain, values))
|
||||
return false;
|
||||
|
||||
chain_pop(chain);
|
||||
|
|
4
config.c
4
config.c
|
@ -340,8 +340,8 @@ conf_to_bar(const struct yml_node *bar)
|
|||
? conf_to_color(mod_foreground) : inherited.foreground,
|
||||
};
|
||||
|
||||
const struct module_info *info = plugin_load_module(mod_name);
|
||||
mods[idx] = info->from_conf(m.value, mod_inherit);
|
||||
const struct module_iface *iface = plugin_load_module(mod_name);
|
||||
mods[idx] = iface->from_conf(m.value, mod_inherit);
|
||||
}
|
||||
|
||||
if (i == 0) {
|
||||
|
|
18
module.h
18
module.h
|
@ -9,18 +9,6 @@
|
|||
#include "yml.h"
|
||||
|
||||
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 {
|
||||
const struct bar *bar;
|
||||
|
@ -47,3 +35,9 @@ struct module {
|
|||
struct module *module_common_new(void);
|
||||
void module_default_destroy(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}
|
||||
|
|
|
@ -266,7 +266,7 @@ alsa_new(const char *card, const char *mixer, struct particle *label)
|
|||
return mod;
|
||||
}
|
||||
|
||||
static struct module *
|
||||
struct module *
|
||||
from_conf(const struct yml_node *node, struct conf_inherit inherited)
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
static bool
|
||||
bool
|
||||
verify_conf(keychain_t *chain, const struct yml_node *node)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
const struct module_info plugin_info = {
|
||||
.verify_conf = &verify_conf,
|
||||
.from_conf = &from_conf,
|
||||
};
|
||||
|
|
|
@ -214,7 +214,7 @@ backlight_new(const char *device, struct particle *label)
|
|||
return mod;
|
||||
}
|
||||
|
||||
static struct module *
|
||||
struct module *
|
||||
from_conf(const struct yml_node *node, struct conf_inherit inherited)
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
static bool
|
||||
bool
|
||||
verify_conf(keychain_t *chain, const struct yml_node *node)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
const struct module_info plugin_info = {
|
||||
.verify_conf = &verify_conf,
|
||||
.from_conf = &from_conf,
|
||||
};
|
||||
|
|
|
@ -348,7 +348,7 @@ battery_new(const char *battery, struct particle *label, int poll_interval_secs)
|
|||
return mod;
|
||||
}
|
||||
|
||||
static struct module *
|
||||
struct module *
|
||||
from_conf(const struct yml_node *node, struct conf_inherit inherited)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
static bool
|
||||
bool
|
||||
verify_conf(keychain_t *chain, const struct yml_node *node)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
const struct module_info plugin_info = {
|
||||
.verify_conf = &verify_conf,
|
||||
.from_conf = &from_conf,
|
||||
};
|
||||
|
|
|
@ -93,7 +93,7 @@ clock_new(struct particle *label, const char *date_format, const char *time_form
|
|||
return mod;
|
||||
}
|
||||
|
||||
static struct module *
|
||||
struct module *
|
||||
from_conf(const struct yml_node *node, struct conf_inherit inherited)
|
||||
{
|
||||
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");
|
||||
}
|
||||
|
||||
static bool
|
||||
bool
|
||||
verify_conf(keychain_t *chain, const struct yml_node *node)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
const struct module_info plugin_info = {
|
||||
.verify_conf = &verify_conf,
|
||||
.from_conf = &from_conf,
|
||||
};
|
||||
|
|
|
@ -639,7 +639,7 @@ i3_new(struct i3_workspaces workspaces[], size_t workspace_count,
|
|||
return mod;
|
||||
}
|
||||
|
||||
static struct module *
|
||||
struct module *
|
||||
from_conf(const struct yml_node *node, struct conf_inherit inherited)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
static bool
|
||||
bool
|
||||
verify_conf(keychain_t *chain, const struct yml_node *node)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
const struct module_info plugin_info = {
|
||||
.verify_conf = &verify_conf,
|
||||
.from_conf = &from_conf,
|
||||
};
|
||||
|
|
|
@ -46,14 +46,14 @@ label_new(struct particle *label)
|
|||
return mod;
|
||||
}
|
||||
|
||||
static struct module *
|
||||
struct module *
|
||||
from_conf(const struct yml_node *node, struct conf_inherit inherited)
|
||||
{
|
||||
const struct yml_node *c = yml_get_value(node, "content");
|
||||
return label_new(conf_to_particle(c, inherited));
|
||||
}
|
||||
|
||||
static bool
|
||||
bool
|
||||
verify_conf(keychain_t *chain, const struct yml_node *node)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
const struct module_info plugin_info = {
|
||||
.verify_conf = &verify_conf,
|
||||
.from_conf = &from_conf,
|
||||
};
|
||||
|
|
|
@ -475,7 +475,7 @@ mpd_new(const char *host, uint16_t port, struct particle *label)
|
|||
return mod;
|
||||
}
|
||||
|
||||
static struct module *
|
||||
struct module *
|
||||
from_conf(const struct yml_node *node, struct conf_inherit inherited)
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
static bool
|
||||
bool
|
||||
verify_conf(keychain_t *chain, const struct yml_node *node)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
const struct module_info plugin_info = {
|
||||
.verify_conf = &verify_conf,
|
||||
.from_conf = &from_conf,
|
||||
};
|
||||
|
|
|
@ -530,7 +530,7 @@ network_new(const char *iface, struct particle *label)
|
|||
return mod;
|
||||
}
|
||||
|
||||
static struct module *
|
||||
struct module *
|
||||
from_conf(const struct yml_node *node, struct conf_inherit inherited)
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
static bool
|
||||
bool
|
||||
verify_conf(keychain_t *chain, const struct yml_node *node)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
const struct module_info plugin_info = {
|
||||
.verify_conf = &verify_conf,
|
||||
.from_conf = &from_conf,
|
||||
};
|
||||
|
|
|
@ -557,7 +557,7 @@ removables_new(struct particle *label, int left_spacing, int right_spacing)
|
|||
return mod;
|
||||
}
|
||||
|
||||
static struct module *
|
||||
struct module *
|
||||
from_conf(const struct yml_node *node, struct conf_inherit inherited)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
static bool
|
||||
bool
|
||||
verify_conf(keychain_t *chain, const struct yml_node *node)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
const struct module_info plugin_info = {
|
||||
.verify_conf = &verify_conf,
|
||||
.from_conf = &from_conf,
|
||||
};
|
||||
|
|
|
@ -467,14 +467,14 @@ xkb_new(struct particle *label)
|
|||
return mod;
|
||||
}
|
||||
|
||||
static struct module *
|
||||
struct module *
|
||||
from_conf(const struct yml_node *node, struct conf_inherit inherited)
|
||||
{
|
||||
const struct yml_node *c = yml_get_value(node, "content");
|
||||
return xkb_new(conf_to_particle(c, inherited));
|
||||
}
|
||||
|
||||
static bool
|
||||
bool
|
||||
verify_conf(keychain_t *chain, const struct yml_node *node)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
const struct module_info plugin_info = {
|
||||
.verify_conf = &verify_conf,
|
||||
.from_conf = &from_conf,
|
||||
};
|
||||
|
|
|
@ -311,14 +311,14 @@ xwindow_new(struct particle *label)
|
|||
return mod;
|
||||
}
|
||||
|
||||
static struct module *
|
||||
struct module *
|
||||
from_conf(const struct yml_node *node, struct conf_inherit inherited)
|
||||
{
|
||||
const struct yml_node *c = yml_get_value(node, "content");
|
||||
return xwindow_new(conf_to_particle(c, inherited));
|
||||
}
|
||||
|
||||
static bool
|
||||
bool
|
||||
verify_conf(keychain_t *chain, const struct yml_node *node)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
const struct module_info plugin_info = {
|
||||
.verify_conf = &verify_conf,
|
||||
.from_conf = &from_conf,
|
||||
};
|
||||
|
|
18
plugin.c
18
plugin.c
|
@ -47,7 +47,8 @@ plugin_load(const char *name, enum plugin_type type)
|
|||
tll_foreach(plugins, plug) {
|
||||
if (plug->item.type == type && strcmp(plug->item.name, name) == 0) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -66,24 +67,19 @@ plugin_load(const char *name, enum plugin_type type)
|
|||
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);
|
||||
|
||||
dlerror(); /* Clear previous error */
|
||||
const char *dl_error = NULL;
|
||||
|
||||
if (type == PLUGIN_MODULE) {
|
||||
plug->sym = dlsym(lib, "plugin_info");
|
||||
dl_error = dlerror();
|
||||
} else {
|
||||
plug->particle.verify_conf = dlsym(lib, "verify_conf");
|
||||
plug->dummy.sym1 = dlsym(lib, "verify_conf");
|
||||
dl_error = dlerror();
|
||||
|
||||
if (dl_error == NULL) {
|
||||
plug->particle.from_conf = dlsym(lib, "from_conf");
|
||||
plug->dummy.sym2 = dlsym(lib, "from_conf");
|
||||
dl_error = dlerror();
|
||||
}
|
||||
}
|
||||
|
||||
if (dl_error != NULL) {
|
||||
LOG_ERR("%s: %s: dlsym: %s", type2str(type), name, dl_error);
|
||||
|
@ -93,11 +89,11 @@ plugin_load(const char *name, enum plugin_type type)
|
|||
return plug;
|
||||
}
|
||||
|
||||
const struct module_info *
|
||||
const struct module_iface *
|
||||
plugin_load_module(const char *name)
|
||||
{
|
||||
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 *
|
||||
|
|
15
plugin.h
15
plugin.h
|
@ -4,13 +4,19 @@
|
|||
#include "module.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 {
|
||||
bool (*verify_conf)(keychain_t *chain, const struct yml_node *node);
|
||||
struct particle *(*from_conf)(
|
||||
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);
|
||||
|
||||
enum plugin_type { PLUGIN_MODULE, PLUGIN_PARTICLE };
|
||||
|
@ -21,7 +27,12 @@ struct plugin {
|
|||
|
||||
void *lib;
|
||||
union {
|
||||
void *sym;
|
||||
struct {
|
||||
void *sym1;
|
||||
void *sym2;
|
||||
} dummy;
|
||||
|
||||
struct module_iface module;
|
||||
struct particle_iface particle;
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue