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