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, see <http://www.gnu.org/licenses/>.
29 #include <pulse/xmalloc.h>
31 #include <pulsecore/source.h>
32 #include <pulsecore/sink.h>
33 #include <pulsecore/core-subscribe.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/macro.h>
39 struct namereg_entry {
40 pa_namereg_type_t type;
45 static bool is_valid_char(char c) {
47 (c >= 'a' && c <= 'z') ||
48 (c >= 'A' && c <= 'Z') ||
49 (c >= '0' && c <= '9') ||
55 bool 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 bool pa_namereg_is_valid_name_or_wildcard(const char *name, pa_namereg_type_t type) {
77 if (pa_namereg_is_valid_name(name))
80 if (type == PA_NAMEREG_SINK &&
81 pa_streq(name, "@DEFAULT_SINK@"))
84 if (type == PA_NAMEREG_SOURCE &&
85 (pa_streq(name, "@DEFAULT_SOURCE@") ||
86 pa_streq(name, "@DEFAULT_MONITOR@")))
92 char* pa_namereg_make_valid_name(const char *name) {
99 n = pa_xnew(char, strlen(name)+1);
101 for (a = name, b = n; *a && (a-name < PA_NAME_MAX); a++, b++)
102 *b = (char) (is_valid_char(*a) ? *a : '_');
109 const char *pa_namereg_register(pa_core *c, const char *name, pa_namereg_type_t type, void *data, bool fail) {
110 struct namereg_entry *e;
120 if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE || type == PA_NAMEREG_CARD) &&
121 !pa_namereg_is_valid_name(name)) {
126 if (!(name = n = pa_namereg_make_valid_name(name)))
130 if ((e = pa_hashmap_get(c->namereg, name)) && fail) {
137 size_t l = strlen(name);
140 if (l+4 > PA_NAME_MAX) {
147 for (i = 2; i <= 99; i++) {
148 pa_snprintf(k, l+4, "%s.%u", name, i);
150 if (!(e = pa_hashmap_get(c->namereg, k)))
164 e = pa_xnew(struct namereg_entry, 1);
166 e->name = n ? n : pa_xstrdup(name);
169 pa_assert_se(pa_hashmap_put(c->namereg, e->name, e) >= 0);
174 void pa_namereg_unregister(pa_core *c, const char *name) {
175 struct namereg_entry *e;
180 pa_assert_se(e = pa_hashmap_remove(c->namereg, name));
185 void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) {
186 struct namereg_entry *e;
190 if (type == PA_NAMEREG_SOURCE && (!name || pa_streq(name, "@DEFAULT_SOURCE@"))) {
191 return c->default_source;
193 } else if (type == PA_NAMEREG_SINK && (!name || pa_streq(name, "@DEFAULT_SINK@"))) {
194 return c->default_sink;
196 } else if (type == PA_NAMEREG_SOURCE && name && pa_streq(name, "@DEFAULT_MONITOR@")) {
198 return c->default_sink->monitor_source;
206 if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE || type == PA_NAMEREG_CARD) &&
207 !pa_namereg_is_valid_name(name))
210 if ((e = pa_hashmap_get(c->namereg, name)))
214 if (pa_atou(name, &idx) < 0)
217 if (type == PA_NAMEREG_SINK)
218 return pa_idxset_get_by_index(c->sinks, idx);
219 else if (type == PA_NAMEREG_SOURCE)
220 return pa_idxset_get_by_index(c->sources, idx);
221 else if (type == PA_NAMEREG_SAMPLE && c->scache)
222 return pa_idxset_get_by_index(c->scache, idx);
223 else if (type == PA_NAMEREG_CARD)
224 return pa_idxset_get_by_index(c->cards, idx);