]> code.delx.au - pulseaudio/blob - src/module.c
main part of the native protocol
[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 module* module_load(struct core *c, const char *name, const char *argument) {
12 struct module *m = NULL;
13 int r;
14
15 assert(c && name);
16
17 m = malloc(sizeof(struct 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, "module_init")))
27 goto fail;
28
29 if (!(m->done = lt_dlsym(m->dl, "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 = idxset_new(NULL, NULL);
41
42 assert(c->modules);
43 r = idxset_put(c->modules, m, &m->index);
44 assert(r >= 0 && m->index != 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 module_free(struct 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 module_unload(struct core *c, struct module *m) {
79 assert(c && m);
80
81 assert(c->modules);
82 if (!(m = idxset_remove_by_data(c->modules, m, NULL)))
83 return;
84
85 module_free(m);
86 }
87
88 void module_unload_by_index(struct core *c, uint32_t index) {
89 struct module *m;
90 assert(c && index != IDXSET_INVALID);
91
92 assert(c->modules);
93 if (!(m = idxset_remove_by_index(c->modules, index)))
94 return;
95
96 module_free(m);
97 }
98
99 void free_callback(void *p, void *userdata) {
100 struct module *m = p;
101 assert(m);
102 module_free(m);
103 }
104
105 void module_unload_all(struct core *c) {
106 assert(c);
107
108 if (!c->modules)
109 return;
110
111 idxset_free(c->modules, free_callback, NULL);
112 c->modules = NULL;
113 }
114
115 char *module_list_to_string(struct core *c) {
116 struct strbuf *s;
117 struct module *m;
118 uint32_t index = IDXSET_INVALID;
119 assert(c);
120
121 s = strbuf_new();
122 assert(s);
123
124 strbuf_printf(s, "%u module(s) loaded.\n", idxset_ncontents(c->modules));
125
126 for (m = idxset_first(c->modules, &index); m; m = idxset_next(c->modules, &index))
127 strbuf_printf(s, " index: %u, name: <%s>, argument: <%s>\n", m->index, m->name, m->argument);
128
129 return strbuf_tostring_free(s);
130 }
131
132
133 struct once_info {
134 struct core *core;
135 uint32_t index;
136 };
137
138
139 void module_unload_once_callback(void *userdata) {
140 struct once_info *i = userdata;
141 assert(i);
142 module_unload_by_index(i->core, i->index);
143 free(i);
144 }
145
146 void module_unload_request(struct core *c, struct 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 }