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 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/autoload.h>
34 #include <pulsecore/source.h>
35 #include <pulsecore/sink.h>
36 #include <pulsecore/core-subscribe.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/macro.h>
42 struct namereg_entry
{
43 pa_namereg_type_t type
;
48 static pa_bool_t
is_valid_char(char c
) {
50 (c
>= 'a' && c
<= 'z') ||
51 (c
>= 'A' && c
<= 'Z') ||
52 (c
>= '0' && c
<= '9') ||
58 pa_bool_t
pa_namereg_is_valid_name(const char *name
) {
64 for (c
= name
; *c
&& (c
-name
< PA_NAME_MAX
); c
++)
65 if (!is_valid_char(*c
))
74 char* pa_namereg_make_valid_name(const char *name
) {
81 n
= pa_xmalloc(strlen(name
)+1);
83 for (a
= name
, b
= n
; *a
&& (a
-name
< PA_NAME_MAX
); a
++, b
++)
84 *b
= (char) (is_valid_char(*a
) ? *a
: '_');
91 void pa_namereg_free(pa_core
*c
) {
97 pa_assert(pa_hashmap_size(c
->namereg
) == 0);
98 pa_hashmap_free(c
->namereg
, NULL
, NULL
);
101 const char *pa_namereg_register(pa_core
*c
, const char *name
, pa_namereg_type_t type
, void *data
, pa_bool_t fail
) {
102 struct namereg_entry
*e
;
112 if ((type
== PA_NAMEREG_SINK
|| type
== PA_NAMEREG_SOURCE
) &&
113 !pa_namereg_is_valid_name(name
)) {
118 if (!(name
= n
= pa_namereg_make_valid_name(name
)))
123 c
->namereg
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
125 if ((e
= pa_hashmap_get(c
->namereg
, name
)) && fail
) {
132 size_t l
= strlen(name
);
135 if (l
+4 > PA_NAME_MAX
) {
142 for (i
= 2; i
<= 99; i
++) {
143 pa_snprintf(k
, l
+4, "%s.%u", name
, i
);
145 if (!(e
= pa_hashmap_get(c
->namereg
, k
)))
159 e
= pa_xnew(struct namereg_entry
, 1);
161 e
->name
= n
? n
: pa_xstrdup(name
);
164 pa_assert_se(pa_hashmap_put(c
->namereg
, e
->name
, e
) >= 0);
169 void pa_namereg_unregister(pa_core
*c
, const char *name
) {
170 struct namereg_entry
*e
;
175 pa_assert_se(e
= pa_hashmap_remove(c
->namereg
, name
));
181 void* pa_namereg_get(pa_core
*c
, const char *name
, pa_namereg_type_t type
, pa_bool_t autoload
) {
182 struct namereg_entry
*e
;
188 if (type
== PA_NAMEREG_SOURCE
)
189 name
= pa_namereg_get_default_source_name(c
);
190 else if (type
== PA_NAMEREG_SINK
)
191 name
= pa_namereg_get_default_sink_name(c
);
193 } else if (strcmp(name
, "@DEFAULT_SINK@") == 0) {
194 if (type
== PA_NAMEREG_SINK
)
195 name
= pa_namereg_get_default_sink_name(c
);
197 } else if (strcmp(name
, "@DEFAULT_SOURCE@") == 0) {
198 if (type
== PA_NAMEREG_SOURCE
)
199 name
= pa_namereg_get_default_source_name(c
);
201 } else if (strcmp(name
, "@DEFAULT_MONITOR@") == 0) {
202 if (type
== PA_NAMEREG_SOURCE
) {
205 if ((k
= pa_namereg_get(c
, NULL
, PA_NAMEREG_SINK
, autoload
)))
206 return k
->monitor_source
;
208 } else if (*name
== '@')
214 if (c
->namereg
&& (e
= pa_hashmap_get(c
->namereg
, name
)))
218 if (pa_atou(name
, &idx
) < 0) {
221 pa_autoload_request(c
, name
, type
);
223 if (c
->namereg
&& (e
= pa_hashmap_get(c
->namereg
, name
)))
231 if (type
== PA_NAMEREG_SINK
)
232 return pa_idxset_get_by_index(c
->sinks
, idx
);
233 else if (type
== PA_NAMEREG_SOURCE
)
234 return pa_idxset_get_by_index(c
->sources
, idx
);
235 else if (type
== PA_NAMEREG_SAMPLE
&& c
->scache
)
236 return pa_idxset_get_by_index(c
->scache
, idx
);
241 int pa_namereg_set_default(pa_core
*c
, const char *name
, pa_namereg_type_t type
) {
245 pa_assert(type
== PA_NAMEREG_SINK
|| type
== PA_NAMEREG_SOURCE
);
247 s
= type
== PA_NAMEREG_SINK
? &c
->default_sink_name
: &c
->default_source_name
;
252 if (name
&& *s
&& !strcmp(name
, *s
))
255 if (!pa_namereg_is_valid_name(name
))
259 *s
= pa_xstrdup(name
);
260 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SERVER
|PA_SUBSCRIPTION_EVENT_CHANGE
, PA_INVALID_INDEX
);
265 const char *pa_namereg_get_default_sink_name(pa_core
*c
) {
270 if (c
->default_sink_name
)
271 return c
->default_sink_name
;
273 if ((s
= pa_idxset_first(c
->sinks
, NULL
)))
274 pa_namereg_set_default(c
, s
->name
, PA_NAMEREG_SINK
);
276 return c
->default_sink_name
;
279 const char *pa_namereg_get_default_source_name(pa_core
*c
) {
285 if (c
->default_source_name
)
286 return c
->default_source_name
;
288 for (s
= pa_idxset_first(c
->sources
, &idx
); s
; s
= pa_idxset_next(c
->sources
, &idx
))
289 if (!s
->monitor_of
) {
290 pa_namereg_set_default(c
, s
->name
, PA_NAMEREG_SOURCE
);
294 if (!c
->default_source_name
)
295 if ((s
= pa_idxset_first(c
->sources
, NULL
)))
296 pa_namereg_set_default(c
, s
->name
, PA_NAMEREG_SOURCE
);
298 return c
->default_source_name
;