]> code.delx.au - pulseaudio/blob - src/module.c
update todo
[pulseaudio] / src / module.c
1 #include <limits.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <assert.h>
5 #include <string.h>
6 #include <errno.h>
7
8 #include "module.h"
9
10 struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char *argument) {
11 struct pa_module *m = NULL;
12 int r;
13
14 assert(c && name);
15
16 m = malloc(sizeof(struct pa_module));
17 assert(m);
18
19 m->name = strdup(name);
20 m->argument = argument ? strdup(argument) : NULL;
21
22 if (!(m->dl = lt_dlopenext(name)))
23 goto fail;
24
25 if (!(m->init = lt_dlsym(m->dl, "pa_module_init")))
26 goto fail;
27
28 if (!(m->done = lt_dlsym(m->dl, "pa_module_done")))
29 goto fail;
30
31 m->userdata = NULL;
32 m->core = c;
33
34 assert(m->init);
35 if (m->init(c, m) < 0)
36 goto fail;
37
38 if (!c->modules)
39 c->modules = pa_idxset_new(NULL, NULL);
40
41 assert(c->modules);
42 r = pa_idxset_put(c->modules, m, &m->index);
43 assert(r >= 0 && m->index != PA_IDXSET_INVALID);
44
45 fprintf(stderr, "module: loaded %u \"%s\" with argument \"%s\".\n", m->index, m->name, m->argument);
46
47 return m;
48
49 fail:
50 if (m) {
51 free(m->argument);
52 free(m->name);
53
54 if (m->dl)
55 lt_dlclose(m->dl);
56
57 free(m);
58 }
59
60 return NULL;
61 }
62
63 static void pa_module_free(struct pa_module *m) {
64 assert(m && m->done && m->core);
65 m->done(m->core, m);
66
67 lt_dlclose(m->dl);
68
69 fprintf(stderr, "module: unloaded %u \"%s\".\n", m->index, m->name);
70
71 free(m->name);
72 free(m->argument);
73 free(m);
74 }
75
76
77 void pa_module_unload(struct pa_core *c, struct pa_module *m) {
78 assert(c && m);
79
80 assert(c->modules);
81 if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
82 return;
83
84 pa_module_free(m);
85 }
86
87 void pa_module_unload_by_index(struct pa_core *c, uint32_t index) {
88 struct pa_module *m;
89 assert(c && index != PA_IDXSET_INVALID);
90
91 assert(c->modules);
92 if (!(m = pa_idxset_remove_by_index(c->modules, index)))
93 return;
94
95 pa_module_free(m);
96 }
97
98 static void free_callback(void *p, void *userdata) {
99 struct pa_module *m = p;
100 assert(m);
101 pa_module_free(m);
102 }
103
104 void pa_module_unload_all(struct pa_core *c) {
105 assert(c);
106
107 if (!c->modules)
108 return;
109
110 pa_idxset_free(c->modules, free_callback, NULL);
111 c->modules = NULL;
112 }
113
114 struct once_info {
115 struct pa_core *core;
116 uint32_t index;
117 };
118
119
120 static void module_unload_once_callback(void *userdata) {
121 struct once_info *i = userdata;
122 assert(i);
123 pa_module_unload_by_index(i->core, i->index);
124 free(i);
125 }
126
127 void pa_module_unload_request(struct pa_core *c, struct pa_module *m) {
128 struct once_info *i;
129 assert(c && m);
130
131 i = malloc(sizeof(struct once_info));
132 assert(i);
133 i->core = c;
134 i->index = m->index;
135 pa_mainloop_api_once(c->mainloop, module_unload_once_callback, i);
136 }