rename configuration file
[profile/ivi/pulseaudio-panda.git] / src / module.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
6   polypaudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   or (at your option) any later version.
10  
11   polypaudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15  
16   You should have received a copy of the GNU General Public License
17   along with polypaudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #include "module.h"
34
35 struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char *argument) {
36     struct pa_module *m = NULL;
37     int r;
38     
39     assert(c && name);
40
41     m = malloc(sizeof(struct pa_module));
42     assert(m);
43
44     m->name = strdup(name);
45     m->argument = argument ? strdup(argument) : NULL;
46     
47     if (!(m->dl = lt_dlopenext(name)))
48         goto fail;
49
50     if (!(m->init = lt_dlsym(m->dl, "pa_module_init")))
51         goto fail;
52
53     if (!(m->done = lt_dlsym(m->dl, "pa_module_done")))
54         goto fail;
55     
56     m->userdata = NULL;
57     m->core = c;
58
59     assert(m->init);
60     if (m->init(c, m) < 0)
61         goto fail;
62
63     if (!c->modules)
64         c->modules = pa_idxset_new(NULL, NULL);
65     
66     assert(c->modules);
67     r = pa_idxset_put(c->modules, m, &m->index);
68     assert(r >= 0 && m->index != PA_IDXSET_INVALID);
69
70     fprintf(stderr, "module: loaded %u \"%s\" with argument \"%s\".\n", m->index, m->name, m->argument);
71     
72     return m;
73     
74 fail:
75     if (m) {
76         free(m->argument);
77         free(m->name);
78         
79         if (m->dl)
80             lt_dlclose(m->dl);
81
82         free(m);
83     }
84
85     return NULL;
86 }
87
88 static void pa_module_free(struct pa_module *m) {
89     assert(m && m->done && m->core);
90     m->done(m->core, m);
91
92     lt_dlclose(m->dl);
93     
94     fprintf(stderr, "module: unloaded %u \"%s\".\n", m->index, m->name);
95
96     free(m->name);
97     free(m->argument);
98     free(m);
99 }
100
101
102 void pa_module_unload(struct pa_core *c, struct pa_module *m) {
103     assert(c && m);
104
105     assert(c->modules);
106     if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
107         return;
108
109     pa_module_free(m);
110 }
111
112 void pa_module_unload_by_index(struct pa_core *c, uint32_t index) {
113     struct pa_module *m;
114     assert(c && index != PA_IDXSET_INVALID);
115
116     assert(c->modules);
117     if (!(m = pa_idxset_remove_by_index(c->modules, index)))
118         return;
119
120     pa_module_free(m);
121 }
122
123 static void free_callback(void *p, void *userdata) {
124     struct pa_module *m = p;
125     assert(m);
126     pa_module_free(m);
127 }
128
129 void pa_module_unload_all(struct pa_core *c) {
130     assert(c);
131
132     if (!c->modules)
133         return;
134
135     pa_idxset_free(c->modules, free_callback, NULL);
136     c->modules = NULL;
137 }
138
139 struct once_info {
140     struct pa_core *core;
141     uint32_t index;
142 };
143     
144
145 static void module_unload_once_callback(void *userdata) {
146     struct once_info *i = userdata;
147     assert(i);
148     pa_module_unload_by_index(i->core, i->index);
149     free(i);
150 }
151
152 void pa_module_unload_request(struct pa_core *c, struct pa_module *m) {
153     struct once_info *i;
154     assert(c && m);
155
156     i = malloc(sizeof(struct once_info));
157     assert(i);
158     i->core = c;
159     i->index = m->index;
160     pa_mainloop_api_once(c->mainloop, module_unload_once_callback, i);
161 }