2004-06-11 17:18:40 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
#include <stdio.h>
|
2004-06-08 23:54:24 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <assert.h>
|
2004-06-10 23:22:16 +00:00
|
|
|
#include <string.h>
|
2004-06-11 21:30:16 +00:00
|
|
|
#include <errno.h>
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char *argument) {
|
|
|
|
|
struct pa_module *m = NULL;
|
2004-06-10 23:22:16 +00:00
|
|
|
int r;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
assert(c && name);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
m = malloc(sizeof(struct pa_module));
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(m);
|
|
|
|
|
|
2004-06-19 01:01:09 +00:00
|
|
|
m->name = strdup(name);
|
|
|
|
|
m->argument = argument ? strdup(argument) : NULL;
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
if (!(m->dl = lt_dlopenext(name)))
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
if (!(m->init = lt_dlsym(m->dl, "pa_module_init")))
|
2004-06-08 23:54:24 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
if (!(m->done = lt_dlsym(m->dl, "pa_module_done")))
|
2004-06-08 23:54:24 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
m->userdata = NULL;
|
2004-06-10 23:22:16 +00:00
|
|
|
m->core = c;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
assert(m->init);
|
|
|
|
|
if (m->init(c, m) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
if (!c->modules)
|
2004-07-03 23:35:12 +00:00
|
|
|
c->modules = pa_idxset_new(NULL, NULL);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
assert(c->modules);
|
2004-07-03 23:35:12 +00:00
|
|
|
r = pa_idxset_put(c->modules, m, &m->index);
|
|
|
|
|
assert(r >= 0 && m->index != PA_IDXSET_INVALID);
|
2004-06-15 15:18:33 +00:00
|
|
|
|
|
|
|
|
fprintf(stderr, "module: loaded %u \"%s\" with argument \"%s\".\n", m->index, m->name, m->argument);
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
return m;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
if (m) {
|
2004-06-11 21:30:16 +00:00
|
|
|
free(m->argument);
|
|
|
|
|
free(m->name);
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
if (m->dl)
|
|
|
|
|
lt_dlclose(m->dl);
|
|
|
|
|
|
|
|
|
|
free(m);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
static void pa_module_free(struct pa_module *m) {
|
2004-06-10 23:22:16 +00:00
|
|
|
assert(m && m->done && m->core);
|
|
|
|
|
m->done(m->core, m);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-06-10 23:22:16 +00:00
|
|
|
lt_dlclose(m->dl);
|
2004-06-15 15:18:33 +00:00
|
|
|
|
|
|
|
|
fprintf(stderr, "module: unloaded %u \"%s\".\n", m->index, m->name);
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
free(m->name);
|
|
|
|
|
free(m->argument);
|
|
|
|
|
free(m);
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-11 17:18:40 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
void pa_module_unload(struct pa_core *c, struct pa_module *m) {
|
2004-06-10 23:22:16 +00:00
|
|
|
assert(c && m);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
assert(c->modules);
|
2004-07-03 23:35:12 +00:00
|
|
|
if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
|
2004-06-08 23:54:24 +00:00
|
|
|
return;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_module_free(m);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
void pa_module_unload_by_index(struct pa_core *c, uint32_t index) {
|
|
|
|
|
struct pa_module *m;
|
|
|
|
|
assert(c && index != PA_IDXSET_INVALID);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
assert(c->modules);
|
2004-07-03 23:35:12 +00:00
|
|
|
if (!(m = pa_idxset_remove_by_index(c->modules, index)))
|
2004-06-08 23:54:24 +00:00
|
|
|
return;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_module_free(m);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
static void free_callback(void *p, void *userdata) {
|
|
|
|
|
struct pa_module *m = p;
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(m);
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_module_free(m);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
void pa_module_unload_all(struct pa_core *c) {
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(c);
|
|
|
|
|
|
|
|
|
|
if (!c->modules)
|
|
|
|
|
return;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_idxset_free(c->modules, free_callback, NULL);
|
2004-06-08 23:54:24 +00:00
|
|
|
c->modules = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-19 01:01:09 +00:00
|
|
|
struct once_info {
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_core *core;
|
2004-06-19 01:01:09 +00:00
|
|
|
uint32_t index;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
static void module_unload_once_callback(void *userdata) {
|
2004-06-19 01:01:09 +00:00
|
|
|
struct once_info *i = userdata;
|
|
|
|
|
assert(i);
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_module_unload_by_index(i->core, i->index);
|
2004-06-19 01:01:09 +00:00
|
|
|
free(i);
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
void pa_module_unload_request(struct pa_core *c, struct pa_module *m) {
|
2004-06-19 01:01:09 +00:00
|
|
|
struct once_info *i;
|
|
|
|
|
assert(c && m);
|
|
|
|
|
|
|
|
|
|
i = malloc(sizeof(struct once_info));
|
|
|
|
|
assert(i);
|
|
|
|
|
i->core = c;
|
|
|
|
|
i->index = m->index;
|
2004-06-23 23:17:30 +00:00
|
|
|
pa_mainloop_api_once(c->mainloop, module_unload_once_callback, i);
|
2004-06-19 01:01:09 +00:00
|
|
|
}
|