default: esd is obsolete, hence don't load it anymore by default
[pulseaudio-mirror.git] / src / pulsecore / namereg.c
blobd98218761e0d00715a7c56d4396695d030b2f06b
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio 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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdlib.h>
27 #include <string.h>
28 #include <string.h>
29 #include <stdio.h>
31 #include <pulse/xmalloc.h>
33 #include <pulsecore/source.h>
34 #include <pulsecore/sink.h>
35 #include <pulsecore/core-subscribe.h>
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/macro.h>
39 #include "namereg.h"
41 struct namereg_entry {
42 pa_namereg_type_t type;
43 char *name;
44 void *data;
47 static pa_bool_t is_valid_char(char c) {
48 return
49 (c >= 'a' && c <= 'z') ||
50 (c >= 'A' && c <= 'Z') ||
51 (c >= '0' && c <= '9') ||
52 c == '.' ||
53 c == '-' ||
54 c == '_';
57 pa_bool_t pa_namereg_is_valid_name(const char *name) {
58 const char *c;
60 pa_assert(name);
62 if (*name == 0)
63 return FALSE;
65 for (c = name; *c && (c-name < PA_NAME_MAX); c++)
66 if (!is_valid_char(*c))
67 return FALSE;
69 if (*c)
70 return FALSE;
72 return TRUE;
75 pa_bool_t pa_namereg_is_valid_name_or_wildcard(const char *name, pa_namereg_type_t type) {
77 pa_assert(name);
79 if (pa_namereg_is_valid_name(name))
80 return TRUE;
82 if (type == PA_NAMEREG_SINK &&
83 pa_streq(name, "@DEFAULT_SINK@"))
84 return TRUE;
86 if (type == PA_NAMEREG_SOURCE &&
87 (pa_streq(name, "@DEFAULT_SOURCE@") ||
88 pa_streq(name, "@DEFAULT_MONITOR@")))
89 return TRUE;
91 return FALSE;
94 char* pa_namereg_make_valid_name(const char *name) {
95 const char *a;
96 char *b, *n;
98 if (*name == 0)
99 return NULL;
101 n = pa_xnew(char, strlen(name)+1);
103 for (a = name, b = n; *a && (a-name < PA_NAME_MAX); a++, b++)
104 *b = (char) (is_valid_char(*a) ? *a : '_');
106 *b = 0;
108 return n;
111 const char *pa_namereg_register(pa_core *c, const char *name, pa_namereg_type_t type, void *data, pa_bool_t fail) {
112 struct namereg_entry *e;
113 char *n = NULL;
115 pa_assert(c);
116 pa_assert(name);
117 pa_assert(data);
119 if (!*name)
120 return NULL;
122 if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE || type == PA_NAMEREG_CARD) &&
123 !pa_namereg_is_valid_name(name)) {
125 if (fail)
126 return NULL;
128 if (!(name = n = pa_namereg_make_valid_name(name)))
129 return NULL;
132 if ((e = pa_hashmap_get(c->namereg, name)) && fail) {
133 pa_xfree(n);
134 return NULL;
137 if (e) {
138 unsigned i;
139 size_t l = strlen(name);
140 char *k;
142 if (l+4 > PA_NAME_MAX) {
143 pa_xfree(n);
144 return NULL;
147 k = pa_xmalloc(l+4);
149 for (i = 2; i <= 99; i++) {
150 pa_snprintf(k, l+4, "%s.%u", name, i);
152 if (!(e = pa_hashmap_get(c->namereg, k)))
153 break;
156 if (e) {
157 pa_xfree(n);
158 pa_xfree(k);
159 return NULL;
162 pa_xfree(n);
163 n = k;
166 e = pa_xnew(struct namereg_entry, 1);
167 e->type = type;
168 e->name = n ? n : pa_xstrdup(name);
169 e->data = data;
171 pa_assert_se(pa_hashmap_put(c->namereg, e->name, e) >= 0);
173 /* If a sink or source is registered and there was none registered
174 * before we inform the clients which then can ask for the default
175 * sink/source which is then assigned. We don't adjust the default
176 * sink/source here right away to give the module the chance to
177 * register more sinks/sources before we choose a new default
178 * sink/source. */
180 if ((!c->default_sink && type == PA_NAMEREG_SINK) ||
181 (!c->default_source && type == PA_NAMEREG_SOURCE))
182 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
184 return e->name;
187 void pa_namereg_unregister(pa_core *c, const char *name) {
188 struct namereg_entry *e;
190 pa_assert(c);
191 pa_assert(name);
193 pa_assert_se(e = pa_hashmap_remove(c->namereg, name));
195 if (c->default_sink == e->data)
196 pa_namereg_set_default_sink(c, NULL);
197 else if (c->default_source == e->data)
198 pa_namereg_set_default_source(c, NULL);
200 pa_xfree(e->name);
201 pa_xfree(e);
204 void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) {
205 struct namereg_entry *e;
206 uint32_t idx;
207 pa_assert(c);
209 if (type == PA_NAMEREG_SOURCE && (!name || pa_streq(name, "@DEFAULT_SOURCE@"))) {
210 pa_source *s;
212 if ((s = pa_namereg_get_default_source(c)))
213 return s;
215 } else if (type == PA_NAMEREG_SINK && (!name || pa_streq(name, "@DEFAULT_SINK@"))) {
216 pa_sink *s;
218 if ((s = pa_namereg_get_default_sink(c)))
219 return s;
221 } else if (type == PA_NAMEREG_SOURCE && name && pa_streq(name, "@DEFAULT_MONITOR@")) {
222 pa_sink *s;
224 if ((s = pa_namereg_get(c, NULL, PA_NAMEREG_SINK)))
225 return s->monitor_source;
228 if (!name)
229 return NULL;
231 if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE || type == PA_NAMEREG_CARD) &&
232 !pa_namereg_is_valid_name(name))
233 return NULL;
235 if ((e = pa_hashmap_get(c->namereg, name)))
236 if (e->type == type)
237 return e->data;
239 if (pa_atou(name, &idx) < 0)
240 return NULL;
242 if (type == PA_NAMEREG_SINK)
243 return pa_idxset_get_by_index(c->sinks, idx);
244 else if (type == PA_NAMEREG_SOURCE)
245 return pa_idxset_get_by_index(c->sources, idx);
246 else if (type == PA_NAMEREG_SAMPLE && c->scache)
247 return pa_idxset_get_by_index(c->scache, idx);
248 else if (type == PA_NAMEREG_CARD)
249 return pa_idxset_get_by_index(c->cards, idx);
251 return NULL;
254 pa_sink* pa_namereg_set_default_sink(pa_core*c, pa_sink *s) {
255 pa_assert(c);
257 if (s && !PA_SINK_IS_LINKED(pa_sink_get_state(s)))
258 return NULL;
260 if (c->default_sink != s) {
261 c->default_sink = s;
262 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
265 return s;
268 pa_source* pa_namereg_set_default_source(pa_core*c, pa_source *s) {
269 pa_assert(c);
271 if (s && !PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
272 return NULL;
274 if (c->default_source != s) {
275 c->default_source = s;
276 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
279 return s;
282 pa_sink *pa_namereg_get_default_sink(pa_core *c) {
283 pa_sink *s, *best = NULL;
284 uint32_t idx;
286 pa_assert(c);
288 if (c->default_sink && PA_SINK_IS_LINKED(pa_sink_get_state(c->default_sink)))
289 return c->default_sink;
291 PA_IDXSET_FOREACH(s, c->sinks, idx)
292 if (PA_SINK_IS_LINKED(pa_sink_get_state(s)))
293 if (!best || s->priority > best->priority)
294 best = s;
296 if (best)
297 return pa_namereg_set_default_sink(c, best);
299 return NULL;
302 pa_source *pa_namereg_get_default_source(pa_core *c) {
303 pa_source *s, *best = NULL;
304 uint32_t idx;
306 pa_assert(c);
308 if (c->default_source && PA_SOURCE_IS_LINKED(pa_source_get_state(c->default_source)))
309 return c->default_source;
311 /* First, try to find one that isn't a monitor */
312 PA_IDXSET_FOREACH(s, c->sources, idx)
313 if (!s->monitor_of && PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
314 if (!best ||
315 s->priority > best->priority)
316 best = s;
318 if (best)
319 return pa_namereg_set_default_source(c, best);
321 /* Then, fallback to a monitor */
322 PA_IDXSET_FOREACH(s, c->sources, idx)
323 if (PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
324 if (!best ||
325 s->priority > best->priority ||
326 (s->priority == best->priority &&
327 s->monitor_of &&
328 best->monitor_of &&
329 s->monitor_of->priority > best->monitor_of->priority))
330 best = s;
332 if (best)
333 return pa_namereg_set_default_source(c, best);
335 return NULL;