]> code.delx.au - pulseaudio/blob - src/pulsecore/namereg.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / namereg.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <string.h>
31 #include <stdio.h>
32
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/autoload.h>
36 #include <pulsecore/source.h>
37 #include <pulsecore/sink.h>
38 #include <pulsecore/core-subscribe.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/macro.h>
41
42 #include "namereg.h"
43
44 struct namereg_entry {
45 pa_namereg_type_t type;
46 char *name;
47 void *data;
48 };
49
50 static int is_valid_char(char c) {
51 return
52 (c >= 'a' && c <= 'z') ||
53 (c >= 'A' && c <= 'Z') ||
54 (c >= '0' && c <= '9') ||
55 c == '.' ||
56 c == '_';
57 }
58
59 static int is_valid_name(const char *name) {
60 const char *c;
61
62 if (*name == 0)
63 return 0;
64
65 for (c = name; *c && (c-name < PA_NAME_MAX); c++)
66 if (!is_valid_char(*c))
67 return 0;
68
69 if (*c)
70 return 0;
71
72 return 1;
73 }
74
75 static char* cleanup_name(const char *name) {
76 const char *a;
77 char *b, *n;
78
79 if (*name == 0)
80 return NULL;
81
82 n = pa_xnew(char, strlen(name)+1);
83
84 for (a = name, b = n; *a && (a-name < PA_NAME_MAX); a++, b++)
85 *b = is_valid_char(*a) ? *a : '_';
86
87 *b = 0;
88
89 return n;
90 }
91
92 void pa_namereg_free(pa_core *c) {
93 pa_assert(c);
94
95 if (!c->namereg)
96 return;
97
98 pa_assert(pa_hashmap_size(c->namereg) == 0);
99 pa_hashmap_free(c->namereg, NULL, NULL);
100 }
101
102 const char *pa_namereg_register(pa_core *c, const char *name, pa_namereg_type_t type, void *data, int fail) {
103 struct namereg_entry *e;
104 char *n = NULL;
105
106 pa_assert(c);
107 pa_assert(name);
108 pa_assert(data);
109
110 if (!*name)
111 return NULL;
112
113 if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE) &&
114 !is_valid_name(name) ) {
115
116 if (fail)
117 return NULL;
118
119 if (!(name = n = cleanup_name(name)))
120 return NULL;
121 }
122
123 if (!c->namereg)
124 c->namereg = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
125
126 if ((e = pa_hashmap_get(c->namereg, name)) && fail) {
127 pa_xfree(n);
128 return NULL;
129 }
130
131 if (e) {
132 unsigned i;
133 size_t l = strlen(name);
134 char *k;
135
136 if (l+4 > PA_NAME_MAX) {
137 pa_xfree(n);
138 return NULL;
139 }
140
141 k = pa_xnew(char, l+4);
142
143 for (i = 2; i <= 99; i++) {
144 pa_snprintf(k, l+4, "%s.%u", name, i);
145
146 if (!(e = pa_hashmap_get(c->namereg, k)))
147 break;
148 }
149
150 if (e) {
151 pa_xfree(n);
152 pa_xfree(k);
153 return NULL;
154 }
155
156 pa_xfree(n);
157 n = k;
158 }
159
160 e = pa_xnew(struct namereg_entry, 1);
161 e->type = type;
162 e->name = n ? n : pa_xstrdup(name);
163 e->data = data;
164
165 pa_assert_se(pa_hashmap_put(c->namereg, e->name, e) >= 0);
166
167 return e->name;
168 }
169
170 void pa_namereg_unregister(pa_core *c, const char *name) {
171 struct namereg_entry *e;
172
173 pa_assert(c);
174 pa_assert(name);
175
176 pa_assert_se(e = pa_hashmap_remove(c->namereg, name));
177
178 pa_xfree(e->name);
179 pa_xfree(e);
180 }
181
182 void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type, int autoload) {
183 struct namereg_entry *e;
184 uint32_t idx;
185 pa_assert(c);
186
187 if (!name) {
188
189 if (type == PA_NAMEREG_SOURCE)
190 name = pa_namereg_get_default_source_name(c);
191 else if (type == PA_NAMEREG_SINK)
192 name = pa_namereg_get_default_sink_name(c);
193
194 } else if (strcmp(name, "@DEFAULT_SINK@") == 0) {
195 if (type == PA_NAMEREG_SINK)
196 name = pa_namereg_get_default_sink_name(c);
197
198 } else if (strcmp(name, "@DEFAULT_SOURCE@") == 0) {
199 if (type == PA_NAMEREG_SOURCE)
200 name = pa_namereg_get_default_source_name(c);
201
202 } else if (strcmp(name, "@DEFAULT_MONITOR@") == 0) {
203 if (type == PA_NAMEREG_SOURCE) {
204 pa_sink *k;
205
206 if ((k = pa_namereg_get(c, NULL, PA_NAMEREG_SINK, autoload)))
207 return k->monitor_source;
208 }
209 } else if (*name == '@')
210 name = NULL;
211
212 if (!name)
213 return NULL;
214
215 if (c->namereg && (e = pa_hashmap_get(c->namereg, name)))
216 if (e->type == type)
217 return e->data;
218
219 if (pa_atou(name, &idx) < 0) {
220
221 if (autoload) {
222 pa_autoload_request(c, name, type);
223
224 if (c->namereg && (e = pa_hashmap_get(c->namereg, name)))
225 if (e->type == type)
226 return e->data;
227 }
228
229 return NULL;
230 }
231
232 if (type == PA_NAMEREG_SINK)
233 return pa_idxset_get_by_index(c->sinks, idx);
234 else if (type == PA_NAMEREG_SOURCE)
235 return pa_idxset_get_by_index(c->sources, idx);
236 else if (type == PA_NAMEREG_SAMPLE && c->scache)
237 return pa_idxset_get_by_index(c->scache, idx);
238
239 return NULL;
240 }
241
242 int pa_namereg_set_default(pa_core*c, const char *name, pa_namereg_type_t type) {
243 char **s;
244
245 pa_assert(c);
246 pa_assert(type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE);
247
248 s = type == PA_NAMEREG_SINK ? &c->default_sink_name : &c->default_source_name;
249
250 if (!name && !*s)
251 return 0;
252
253 if (name && *s && !strcmp(name, *s))
254 return 0;
255
256 if (!is_valid_name(name))
257 return -1;
258
259 pa_xfree(*s);
260 *s = pa_xstrdup(name);
261 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
262
263 return 0;
264 }
265
266 const char *pa_namereg_get_default_sink_name(pa_core *c) {
267 pa_sink *s;
268
269 pa_assert(c);
270
271 if (c->default_sink_name)
272 return c->default_sink_name;
273
274 if ((s = pa_idxset_first(c->sinks, NULL)))
275 pa_namereg_set_default(c, s->name, PA_NAMEREG_SINK);
276
277 return c->default_sink_name;
278 }
279
280 const char *pa_namereg_get_default_source_name(pa_core *c) {
281 pa_source *s;
282 uint32_t idx;
283
284 pa_assert(c);
285
286 if (c->default_source_name)
287 return c->default_source_name;
288
289 for (s = pa_idxset_first(c->sources, &idx); s; s = pa_idxset_next(c->sources, &idx))
290 if (!s->monitor_of) {
291 pa_namereg_set_default(c, s->name, PA_NAMEREG_SOURCE);
292 break;
293 }
294
295 if (!c->default_source_name)
296 if ((s = pa_idxset_first(c->sources, NULL)))
297 pa_namereg_set_default(c, s->name, PA_NAMEREG_SOURCE);
298
299 return c->default_source_name;
300 }