]> code.delx.au - pulseaudio/blob - src/pulsecore/namereg.c
Merge commit '12db687acf3befe485bfff3700111999c95247fa'
[pulseaudio] / src / pulsecore / namereg.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
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.
10
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.
15
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 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <string.h>
29 #include <stdio.h>
30
31 #include <pulse/xmalloc.h>
32
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>
38
39 #include "namereg.h"
40
41 struct namereg_entry {
42 pa_namereg_type_t type;
43 char *name;
44 void *data;
45 };
46
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 == '_';
55 }
56
57 pa_bool_t pa_namereg_is_valid_name(const char *name) {
58 const char *c;
59
60 if (*name == 0)
61 return FALSE;
62
63 for (c = name; *c && (c-name < PA_NAME_MAX); c++)
64 if (!is_valid_char(*c))
65 return FALSE;
66
67 if (*c)
68 return FALSE;
69
70 return TRUE;
71 }
72
73 char* pa_namereg_make_valid_name(const char *name) {
74 const char *a;
75 char *b, *n;
76
77 if (*name == 0)
78 return NULL;
79
80 n = pa_xmalloc(strlen(name)+1);
81
82 for (a = name, b = n; *a && (a-name < PA_NAME_MAX); a++, b++)
83 *b = (char) (is_valid_char(*a) ? *a : '_');
84
85 *b = 0;
86
87 return n;
88 }
89
90 void pa_namereg_free(pa_core *c) {
91 pa_assert(c);
92
93 if (!c->namereg)
94 return;
95
96 pa_assert(pa_hashmap_size(c->namereg) == 0);
97 pa_hashmap_free(c->namereg, NULL, NULL);
98 }
99
100 const char *pa_namereg_register(pa_core *c, const char *name, pa_namereg_type_t type, void *data, pa_bool_t fail) {
101 struct namereg_entry *e;
102 char *n = NULL;
103
104 pa_assert(c);
105 pa_assert(name);
106 pa_assert(data);
107
108 if (!*name)
109 return NULL;
110
111 if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE || type == PA_NAMEREG_CARD) &&
112 !pa_namereg_is_valid_name(name)) {
113
114 if (fail)
115 return NULL;
116
117 if (!(name = n = pa_namereg_make_valid_name(name)))
118 return NULL;
119 }
120
121 if (!c->namereg)
122 c->namereg = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
123
124 if ((e = pa_hashmap_get(c->namereg, name)) && fail) {
125 pa_xfree(n);
126 return NULL;
127 }
128
129 if (e) {
130 unsigned i;
131 size_t l = strlen(name);
132 char *k;
133
134 if (l+4 > PA_NAME_MAX) {
135 pa_xfree(n);
136 return NULL;
137 }
138
139 k = pa_xmalloc(l+4);
140
141 for (i = 2; i <= 99; i++) {
142 pa_snprintf(k, l+4, "%s.%u", name, i);
143
144 if (!(e = pa_hashmap_get(c->namereg, k)))
145 break;
146 }
147
148 if (e) {
149 pa_xfree(n);
150 pa_xfree(k);
151 return NULL;
152 }
153
154 pa_xfree(n);
155 n = k;
156 }
157
158 e = pa_xnew(struct namereg_entry, 1);
159 e->type = type;
160 e->name = n ? n : pa_xstrdup(name);
161 e->data = data;
162
163 pa_assert_se(pa_hashmap_put(c->namereg, e->name, e) >= 0);
164
165 return e->name;
166 }
167
168 void pa_namereg_unregister(pa_core *c, const char *name) {
169 struct namereg_entry *e;
170
171 pa_assert(c);
172 pa_assert(name);
173
174 pa_assert_se(e = pa_hashmap_remove(c->namereg, name));
175
176 if (c->default_sink == e->data)
177 pa_namereg_set_default_sink(c, NULL);
178 else if (c->default_source == e->data)
179 pa_namereg_set_default_source(c, NULL);
180
181 pa_xfree(e->name);
182 pa_xfree(e);
183 }
184
185 void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) {
186 struct namereg_entry *e;
187 uint32_t idx;
188 pa_assert(c);
189
190 if (type == PA_NAMEREG_SOURCE && (!name || pa_streq(name, "@DEFAULT_SOURCE@"))) {
191 pa_source *s;
192
193 if ((s = pa_namereg_get_default_source(c)))
194 return s;
195
196 } else if (type == PA_NAMEREG_SINK && (!name || pa_streq(name, "@DEFAULT_SINK@"))) {
197 pa_sink *s;
198
199 if ((s = pa_namereg_get_default_sink(c)))
200 return s;
201
202 } else if (type == PA_NAMEREG_SOURCE && name && pa_streq(name, "@DEFAULT_MONITOR@")) {
203 pa_sink *s;
204
205 if ((s = pa_namereg_get(c, NULL, PA_NAMEREG_SINK)))
206 return s->monitor_source;
207
208 }
209
210 if (*name == '@' || !name || !pa_namereg_is_valid_name(name))
211 return NULL;
212
213 if (c->namereg && (e = pa_hashmap_get(c->namereg, name)))
214 if (e->type == type)
215 return e->data;
216
217 if (pa_atou(name, &idx) < 0)
218 return NULL;
219
220 if (type == PA_NAMEREG_SINK)
221 return pa_idxset_get_by_index(c->sinks, idx);
222 else if (type == PA_NAMEREG_SOURCE)
223 return pa_idxset_get_by_index(c->sources, idx);
224 else if (type == PA_NAMEREG_SAMPLE && c->scache)
225 return pa_idxset_get_by_index(c->scache, idx);
226 else if (type == PA_NAMEREG_CARD)
227 return pa_idxset_get_by_index(c->cards, idx);
228
229 return NULL;
230 }
231
232 pa_sink* pa_namereg_set_default_sink(pa_core*c, pa_sink *s) {
233 pa_assert(c);
234
235 if (c->default_sink != s) {
236 c->default_sink = s;
237 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
238 }
239
240 return s;
241 }
242
243 pa_source* pa_namereg_set_default_source(pa_core*c, pa_source *s) {
244 pa_assert(c);
245
246 if (c->default_source != s) {
247 c->default_source = s;
248 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
249 }
250
251 return s;
252 }
253
254 pa_sink *pa_namereg_get_default_sink(pa_core *c) {
255 pa_sink *s;
256
257 pa_assert(c);
258
259 if (c->default_sink)
260 return c->default_sink;
261
262 if ((s = pa_idxset_first(c->sinks, NULL)))
263 return pa_namereg_set_default_sink(c, s);
264
265 return NULL;
266 }
267
268 pa_source *pa_namereg_get_default_source(pa_core *c) {
269 pa_source *s;
270 uint32_t idx;
271
272 pa_assert(c);
273
274 if (c->default_source)
275 return c->default_source;
276
277 for (s = PA_SOURCE(pa_idxset_first(c->sources, &idx)); s; s = PA_SOURCE(pa_idxset_next(c->sources, &idx)))
278 if (!s->monitor_of)
279 return pa_namereg_set_default_source(c, s);
280
281 if ((s = pa_idxset_first(c->sources, NULL)))
282 return pa_namereg_set_default_source(c, s);
283
284 return NULL;
285 }