]> code.delx.au - pulseaudio/blob - src/pulsecore/namereg.c
Merge branch 'master' of git://0pointer.de/pulseaudio into dbus-work
[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.1 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 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;
92 char *n = NULL;
93
94 pa_assert(c);
95 pa_assert(name);
96 pa_assert(data);
97
98 if (!*name)
99 return NULL;
100
101 if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE || type == PA_NAMEREG_CARD) &&
102 !pa_namereg_is_valid_name(name)) {
103
104 if (fail)
105 return NULL;
106
107 if (!(name = n = pa_namereg_make_valid_name(name)))
108 return NULL;
109 }
110
111 if ((e = pa_hashmap_get(c->namereg, name)) && fail) {
112 pa_xfree(n);
113 return NULL;
114 }
115
116 if (e) {
117 unsigned i;
118 size_t l = strlen(name);
119 char *k;
120
121 if (l+4 > PA_NAME_MAX) {
122 pa_xfree(n);
123 return NULL;
124 }
125
126 k = pa_xmalloc(l+4);
127
128 for (i = 2; i <= 99; i++) {
129 pa_snprintf(k, l+4, "%s.%u", name, i);
130
131 if (!(e = pa_hashmap_get(c->namereg, k)))
132 break;
133 }
134
135 if (e) {
136 pa_xfree(n);
137 pa_xfree(k);
138 return NULL;
139 }
140
141 pa_xfree(n);
142 n = k;
143 }
144
145 e = pa_xnew(struct namereg_entry, 1);
146 e->type = type;
147 e->name = n ? n : pa_xstrdup(name);
148 e->data = data;
149
150 pa_assert_se(pa_hashmap_put(c->namereg, e->name, e) >= 0);
151
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);
156
157 return e->name;
158 }
159
160 void pa_namereg_unregister(pa_core *c, const char *name) {
161 struct namereg_entry *e;
162 uint32_t idx;
163
164 pa_assert(c);
165 pa_assert(name);
166
167 pa_assert_se(e = pa_hashmap_remove(c->namereg, name));
168
169 if (c->default_sink == e->data) {
170 pa_sink *new_default = NULL;
171
172 /* FIXME: the selection here should be based priority values on
173 * the sinks */
174
175 PA_IDXSET_FOREACH(new_default, c->sinks, idx) {
176 if (new_default != e->data && PA_SINK_IS_LINKED(pa_sink_get_state(new_default)))
177 break;
178 }
179
180 pa_namereg_set_default_sink(c, new_default);
181
182 } else if (c->default_source == e->data) {
183 pa_source *new_default = NULL;
184
185 /* First, try to find one that isn't a monitor */
186 PA_IDXSET_FOREACH(new_default, c->sources, idx) {
187 if (new_default != e->data && !new_default->monitor_of && PA_SOURCE_IS_LINKED(pa_source_get_state(new_default)))
188 break;
189 }
190
191 if (!new_default) {
192 /* Then, fallback to a monitor */
193 PA_IDXSET_FOREACH(new_default, c->sources, idx) {
194 if (new_default != e->data && PA_SOURCE_IS_LINKED(pa_source_get_state(new_default)))
195 break;
196 }
197 }
198
199 pa_namereg_set_default_source(c, new_default);
200 }
201
202 pa_xfree(e->name);
203 pa_xfree(e);
204 }
205
206 void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) {
207 struct namereg_entry *e;
208 uint32_t idx;
209 pa_assert(c);
210
211 if (type == PA_NAMEREG_SOURCE && (!name || pa_streq(name, "@DEFAULT_SOURCE@"))) {
212 pa_source *s;
213
214 if ((s = pa_namereg_get_default_source(c)))
215 return s;
216
217 } else if (type == PA_NAMEREG_SINK && (!name || pa_streq(name, "@DEFAULT_SINK@"))) {
218 pa_sink *s;
219
220 if ((s = pa_namereg_get_default_sink(c)))
221 return s;
222
223 } else if (type == PA_NAMEREG_SOURCE && name && pa_streq(name, "@DEFAULT_MONITOR@")) {
224 pa_sink *s;
225
226 if ((s = pa_namereg_get(c, NULL, PA_NAMEREG_SINK)))
227 return s->monitor_source;
228 }
229
230 if (!name)
231 return NULL;
232
233 if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE || type == PA_NAMEREG_CARD) &&
234 !pa_namereg_is_valid_name(name))
235 return NULL;
236
237 if ((e = pa_hashmap_get(c->namereg, name)))
238 if (e->type == type)
239 return e->data;
240
241 if (pa_atou(name, &idx) < 0)
242 return NULL;
243
244 if (type == PA_NAMEREG_SINK)
245 return pa_idxset_get_by_index(c->sinks, idx);
246 else if (type == PA_NAMEREG_SOURCE)
247 return pa_idxset_get_by_index(c->sources, idx);
248 else if (type == PA_NAMEREG_SAMPLE && c->scache)
249 return pa_idxset_get_by_index(c->scache, idx);
250 else if (type == PA_NAMEREG_CARD)
251 return pa_idxset_get_by_index(c->cards, idx);
252
253 return NULL;
254 }
255
256 pa_sink* pa_namereg_set_default_sink(pa_core*c, pa_sink *s) {
257 pa_assert(c);
258
259 if (s && !PA_SINK_IS_LINKED(pa_sink_get_state(s)))
260 return NULL;
261
262 if (c->default_sink != s) {
263 c->default_sink = s;
264 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
265 }
266
267 return s;
268 }
269
270 pa_source* pa_namereg_set_default_source(pa_core*c, pa_source *s) {
271 pa_assert(c);
272
273 if (s && !PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
274 return NULL;
275
276 if (c->default_source != s) {
277 c->default_source = s;
278 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
279 }
280
281 return s;
282 }
283
284 /* XXX: After removing old functionality, has this function become useless? */
285 pa_sink *pa_namereg_get_default_sink(pa_core *c) {
286 pa_sink *s;
287 uint32_t idx;
288
289 pa_assert(c);
290
291 if (!c->default_sink || PA_SINK_IS_LINKED(pa_sink_get_state(c->default_sink)))
292 return c->default_sink;
293
294 /* The old default sink has become unlinked, set a new one. */
295
296 /* FIXME: the selection here should be based priority values on
297 * the sinks */
298
299 PA_IDXSET_FOREACH(s, c->sinks, idx)
300 if (PA_SINK_IS_LINKED(pa_sink_get_state(s)))
301 return pa_namereg_set_default_sink(c, s);
302
303 return pa_namereg_set_default_sink(c, NULL);
304 }
305
306 /* XXX: After removing old functionality, has this function become useless? */
307 pa_source *pa_namereg_get_default_source(pa_core *c) {
308 pa_source *s;
309 uint32_t idx;
310
311 pa_assert(c);
312
313 if (!c->default_source || PA_SOURCE_IS_LINKED(pa_source_get_state(c->default_source)))
314 return c->default_source;
315
316 /* The old default source has become unlinked, set a new one. */
317
318 /* First, try to find one that isn't a monitor */
319 PA_IDXSET_FOREACH(s, c->sources, idx)
320 if (!s->monitor_of && PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
321 return pa_namereg_set_default_source(c, s);
322
323 /* Then, fallback to a monitor */
324 PA_IDXSET_FOREACH(s, c->sources, idx)
325 if (PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
326 return pa_namereg_set_default_source(c, s);
327
328 return pa_namereg_set_default_source(c, NULL);
329 }