]> code.delx.au - pulseaudio/blob - src/pulsecore/autoload.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / autoload.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/module.h>
35 #include <pulsecore/memchunk.h>
36 #include <pulsecore/sound-file.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/core-scache.h>
40 #include <pulsecore/core-subscribe.h>
41
42 #include "autoload.h"
43
44 static void entry_free(pa_autoload_entry *e) {
45 pa_assert(e);
46 pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_AUTOLOAD|PA_SUBSCRIPTION_EVENT_REMOVE, PA_INVALID_INDEX);
47 pa_xfree(e->name);
48 pa_xfree(e->module);
49 pa_xfree(e->argument);
50 pa_xfree(e);
51 }
52
53 static void entry_remove_and_free(pa_autoload_entry *e) {
54 pa_assert(e);
55 pa_assert(e->core);
56
57 pa_idxset_remove_by_data(e->core->autoload_idxset, e, NULL);
58 pa_hashmap_remove(e->core->autoload_hashmap, e->name);
59 entry_free(e);
60 }
61
62 static pa_autoload_entry* entry_new(pa_core *c, const char *name) {
63 pa_autoload_entry *e = NULL;
64
65 pa_core_assert_ref(c);
66 pa_assert(name);
67
68 if (c->autoload_hashmap && (e = pa_hashmap_get(c->autoload_hashmap, name)))
69 return NULL;
70
71 e = pa_xnew(pa_autoload_entry, 1);
72 e->core = c;
73 e->name = pa_xstrdup(name);
74 e->module = e->argument = NULL;
75 e->in_action = 0;
76
77 if (!c->autoload_hashmap)
78 c->autoload_hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
79 pa_assert(c->autoload_hashmap);
80
81 pa_hashmap_put(c->autoload_hashmap, e->name, e);
82
83 if (!c->autoload_idxset)
84 c->autoload_idxset = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
85 pa_idxset_put(c->autoload_idxset, e, &e->index);
86
87 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_AUTOLOAD|PA_SUBSCRIPTION_EVENT_NEW, e->index);
88
89 return e;
90 }
91
92 int pa_autoload_add(pa_core *c, const char*name, pa_namereg_type_t type, const char*module, const char *argument, uint32_t *idx) {
93 pa_autoload_entry *e = NULL;
94
95 pa_assert(c);
96 pa_assert(name);
97 pa_assert(module);
98 pa_assert(type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE);
99
100 if (!(e = entry_new(c, name)))
101 return -1;
102
103 e->module = pa_xstrdup(module);
104 e->argument = pa_xstrdup(argument);
105 e->type = type;
106
107 if (idx)
108 *idx = e->index;
109
110 return 0;
111 }
112
113 int pa_autoload_remove_by_name(pa_core *c, const char*name, pa_namereg_type_t type) {
114 pa_autoload_entry *e;
115
116 pa_assert(c);
117 pa_assert(name);
118 pa_assert(type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE);
119
120 if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || e->type != type)
121 return -1;
122
123 entry_remove_and_free(e);
124 return 0;
125 }
126
127 int pa_autoload_remove_by_index(pa_core *c, uint32_t idx) {
128 pa_autoload_entry *e;
129
130 pa_assert(c);
131 pa_assert(idx != PA_IDXSET_INVALID);
132
133 if (!c->autoload_idxset || !(e = pa_idxset_get_by_index(c->autoload_idxset, idx)))
134 return -1;
135
136 entry_remove_and_free(e);
137 return 0;
138 }
139
140 void pa_autoload_request(pa_core *c, const char *name, pa_namereg_type_t type) {
141 pa_autoload_entry *e;
142 pa_module *m;
143
144 pa_assert(c);
145 pa_assert(name);
146
147 if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || (e->type != type))
148 return;
149
150 if (e->in_action)
151 return;
152
153 e->in_action = 1;
154
155 if (type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE) {
156 if ((m = pa_module_load(c, e->module, e->argument)))
157 m->auto_unload = 1;
158 }
159
160 e->in_action = 0;
161 }
162
163 static void free_func(void *p, PA_GCC_UNUSED void *userdata) {
164 pa_autoload_entry *e = p;
165 pa_idxset_remove_by_data(e->core->autoload_idxset, e, NULL);
166 entry_free(e);
167 }
168
169 void pa_autoload_free(pa_core *c) {
170
171 if (c->autoload_hashmap) {
172 pa_hashmap_free(c->autoload_hashmap, free_func, NULL);
173 c->autoload_hashmap = NULL;
174 }
175
176 if (c->autoload_idxset) {
177 pa_idxset_free(c->autoload_idxset, NULL, NULL);
178 c->autoload_idxset = NULL;
179 }
180 }
181
182 const pa_autoload_entry* pa_autoload_get_by_name(pa_core *c, const char*name, pa_namereg_type_t type) {
183 pa_autoload_entry *e;
184
185 pa_core_assert_ref(c);
186 pa_assert(name);
187
188 if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || e->type != type)
189 return NULL;
190
191 return e;
192 }
193
194 const pa_autoload_entry* pa_autoload_get_by_index(pa_core *c, uint32_t idx) {
195 pa_autoload_entry *e;
196
197 pa_core_assert_ref(c);
198 pa_assert(idx != PA_IDXSET_INVALID);
199
200 if (!c->autoload_idxset || !(e = pa_idxset_get_by_index(c->autoload_idxset, idx)))
201 return NULL;
202
203 return e;
204 }