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
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>
41 struct namereg_entry
{
42 pa_namereg_type_t type
;
47 static pa_bool_t
is_valid_char(char c
) {
49 (c
>= 'a' && c
<= 'z') ||
50 (c
>= 'A' && c
<= 'Z') ||
51 (c
>= '0' && c
<= '9') ||
57 pa_bool_t
pa_namereg_is_valid_name(const char *name
) {
65 for (c
= name
; *c
&& (c
-name
< PA_NAME_MAX
); c
++)
66 if (!is_valid_char(*c
))
75 pa_bool_t
pa_namereg_is_valid_name_or_wildcard(const char *name
, pa_namereg_type_t type
) {
79 if (pa_namereg_is_valid_name(name
))
82 if (type
== PA_NAMEREG_SINK
&&
83 pa_streq(name
, "@DEFAULT_SINK@"))
86 if (type
== PA_NAMEREG_SOURCE
&&
87 (pa_streq(name
, "@DEFAULT_SOURCE@") ||
88 pa_streq(name
, "@DEFAULT_MONITOR@")))
94 char* pa_namereg_make_valid_name(const char *name
) {
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
: '_');
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
;
122 if ((type
== PA_NAMEREG_SINK
|| type
== PA_NAMEREG_SOURCE
|| type
== PA_NAMEREG_CARD
) &&
123 !pa_namereg_is_valid_name(name
)) {
128 if (!(name
= n
= pa_namereg_make_valid_name(name
)))
132 if ((e
= pa_hashmap_get(c
->namereg
, name
)) && fail
) {
139 size_t l
= strlen(name
);
142 if (l
+4 > PA_NAME_MAX
) {
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
)))
166 e
= pa_xnew(struct namereg_entry
, 1);
168 e
->name
= n
? n
: pa_xstrdup(name
);
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
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
);
187 void pa_namereg_unregister(pa_core
*c
, const char *name
) {
188 struct namereg_entry
*e
;
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
);
204 void* pa_namereg_get(pa_core
*c
, const char *name
, pa_namereg_type_t type
) {
205 struct namereg_entry
*e
;
209 if (type
== PA_NAMEREG_SOURCE
&& (!name
|| pa_streq(name
, "@DEFAULT_SOURCE@"))) {
212 if ((s
= pa_namereg_get_default_source(c
)))
215 } else if (type
== PA_NAMEREG_SINK
&& (!name
|| pa_streq(name
, "@DEFAULT_SINK@"))) {
218 if ((s
= pa_namereg_get_default_sink(c
)))
221 } else if (type
== PA_NAMEREG_SOURCE
&& name
&& pa_streq(name
, "@DEFAULT_MONITOR@")) {
224 if ((s
= pa_namereg_get(c
, NULL
, PA_NAMEREG_SINK
)))
225 return s
->monitor_source
;
231 if ((type
== PA_NAMEREG_SINK
|| type
== PA_NAMEREG_SOURCE
|| type
== PA_NAMEREG_CARD
) &&
232 !pa_namereg_is_valid_name(name
))
235 if ((e
= pa_hashmap_get(c
->namereg
, name
)))
239 if (pa_atou(name
, &idx
) < 0)
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
);
254 pa_sink
* pa_namereg_set_default_sink(pa_core
*c
, pa_sink
*s
) {
257 if (s
&& !PA_SINK_IS_LINKED(pa_sink_get_state(s
)))
260 if (c
->default_sink
!= s
) {
262 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SERVER
|PA_SUBSCRIPTION_EVENT_CHANGE
, PA_INVALID_INDEX
);
268 pa_source
* pa_namereg_set_default_source(pa_core
*c
, pa_source
*s
) {
271 if (s
&& !PA_SOURCE_IS_LINKED(pa_source_get_state(s
)))
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
);
282 pa_sink
*pa_namereg_get_default_sink(pa_core
*c
) {
283 pa_sink
*s
, *best
= NULL
;
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
)
297 return pa_namereg_set_default_sink(c
, best
);
302 pa_source
*pa_namereg_get_default_source(pa_core
*c
) {
303 pa_source
*s
, *best
= NULL
;
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
)))
315 s
->priority
> best
->priority
)
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
)))
325 s
->priority
> best
->priority
||
326 (s
->priority
== best
->priority
&&
329 s
->monitor_of
->priority
> best
->monitor_of
->priority
))
333 return pa_namereg_set_default_source(c
, best
);