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
) {
63 for (c
= name
; *c
&& (c
-name
< PA_NAME_MAX
); c
++)
64 if (!is_valid_char(*c
))
73 char* pa_namereg_make_valid_name(const char *name
) {
80 n
= pa_xmalloc(strlen(name
)+1);
82 for (a
= name
, b
= n
; *a
&& (a
-name
< PA_NAME_MAX
); a
++, b
++)
83 *b
= (char) (is_valid_char(*a
) ? *a
: '_');
90 const char *pa_namereg_register(pa_core
*c
, const char *name
, pa_namereg_type_t type
, void *data
, pa_bool_t fail
) {
91 struct namereg_entry
*e
;
101 if ((type
== PA_NAMEREG_SINK
|| type
== PA_NAMEREG_SOURCE
|| type
== PA_NAMEREG_CARD
) &&
102 !pa_namereg_is_valid_name(name
)) {
107 if (!(name
= n
= pa_namereg_make_valid_name(name
)))
111 if ((e
= pa_hashmap_get(c
->namereg
, name
)) && fail
) {
118 size_t l
= strlen(name
);
121 if (l
+4 > PA_NAME_MAX
) {
128 for (i
= 2; i
<= 99; i
++) {
129 pa_snprintf(k
, l
+4, "%s.%u", name
, i
);
131 if (!(e
= pa_hashmap_get(c
->namereg
, k
)))
145 e
= pa_xnew(struct namereg_entry
, 1);
147 e
->name
= n
? n
: pa_xstrdup(name
);
150 pa_assert_se(pa_hashmap_put(c
->namereg
, e
->name
, e
) >= 0);
152 if (type
== PA_NAMEREG_SINK
&& !c
->default_sink
)
153 pa_namereg_set_default_sink(c
, data
);
154 else if (type
== PA_NAMEREG_SOURCE
&& !c
->default_source
)
155 pa_namereg_set_default_source(c
, data
);
160 void pa_namereg_unregister(pa_core
*c
, const char *name
) {
161 struct namereg_entry
*e
;
167 pa_assert_se(e
= pa_hashmap_remove(c
->namereg
, name
));
169 if (c
->default_sink
== e
->data
) {
170 pa_sink
*new_default
= pa_idxset_first(c
->sinks
, &idx
);
172 if (new_default
== e
->data
)
173 new_default
= pa_idxset_next(c
->sinks
, &idx
);
175 pa_namereg_set_default_sink(c
, new_default
);
177 } else if (c
->default_source
== e
->data
) {
178 pa_source
*new_default
;
180 for (new_default
= pa_idxset_first(c
->sources
, &idx
); new_default
; new_default
= pa_idxset_next(c
->sources
, &idx
)) {
181 if (new_default
!= e
->data
&& !new_default
->monitor_of
)
186 new_default
= pa_idxset_first(c
->sources
, &idx
);
188 if (new_default
== e
->data
)
189 new_default
= pa_idxset_next(c
->sources
, &idx
);
192 pa_namereg_set_default_source(c
, new_default
);
199 void* pa_namereg_get(pa_core
*c
, const char *name
, pa_namereg_type_t type
) {
200 struct namereg_entry
*e
;
204 if (type
== PA_NAMEREG_SOURCE
&& (!name
|| pa_streq(name
, "@DEFAULT_SOURCE@"))) {
207 if ((s
= pa_namereg_get_default_source(c
)))
210 } else if (type
== PA_NAMEREG_SINK
&& (!name
|| pa_streq(name
, "@DEFAULT_SINK@"))) {
213 if ((s
= pa_namereg_get_default_sink(c
)))
216 } else if (type
== PA_NAMEREG_SOURCE
&& name
&& pa_streq(name
, "@DEFAULT_MONITOR@")) {
219 if ((s
= pa_namereg_get(c
, NULL
, PA_NAMEREG_SINK
)))
220 return s
->monitor_source
;
226 if ((type
== PA_NAMEREG_SINK
|| type
== PA_NAMEREG_SOURCE
|| type
== PA_NAMEREG_CARD
) &&
227 !pa_namereg_is_valid_name(name
))
230 if ((e
= pa_hashmap_get(c
->namereg
, name
)))
234 if (pa_atou(name
, &idx
) < 0)
237 if (type
== PA_NAMEREG_SINK
)
238 return pa_idxset_get_by_index(c
->sinks
, idx
);
239 else if (type
== PA_NAMEREG_SOURCE
)
240 return pa_idxset_get_by_index(c
->sources
, idx
);
241 else if (type
== PA_NAMEREG_SAMPLE
&& c
->scache
)
242 return pa_idxset_get_by_index(c
->scache
, idx
);
243 else if (type
== PA_NAMEREG_CARD
)
244 return pa_idxset_get_by_index(c
->cards
, idx
);
249 pa_sink
* pa_namereg_set_default_sink(pa_core
*c
, pa_sink
*s
) {
252 if (c
->default_sink
!= s
) {
254 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SERVER
|PA_SUBSCRIPTION_EVENT_CHANGE
, PA_INVALID_INDEX
);
260 pa_source
* pa_namereg_set_default_source(pa_core
*c
, pa_source
*s
) {
263 if (c
->default_source
!= s
) {
264 c
->default_source
= s
;
265 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SERVER
|PA_SUBSCRIPTION_EVENT_CHANGE
, PA_INVALID_INDEX
);
271 /* XXX: After removing old functionality, has this function become useless? */
272 pa_sink
*pa_namereg_get_default_sink(pa_core
*c
) {
275 return c
->default_sink
;
278 /* XXX: After removing old functionality, has this function become useless? */
279 pa_source
*pa_namereg_get_default_source(pa_core
*c
) {
282 return c
->default_source
;