]> code.delx.au - pulseaudio/blob - src/pulsecore/namereg.c
7f66af0595ec8caccb51a00d66d1a39d82685be0
[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 <assert.h>
31 #include <string.h>
32 #include <stdio.h>
33
34 #include <pulse/xmalloc.h>
35
36 #include <pulsecore/autoload.h>
37 #include <pulsecore/source.h>
38 #include <pulsecore/sink.h>
39 #include <pulsecore/core-subscribe.h>
40 #include <pulsecore/core-util.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 assert(c);
94
95 if (!c->namereg)
96 return;
97
98 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 int r;
106
107 assert(c);
108 assert(name);
109 assert(data);
110
111 if (!*name)
112 return NULL;
113
114 if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE) &&
115 !is_valid_name(name) ) {
116
117 if (fail)
118 return NULL;
119
120 if (!(name = n = cleanup_name(name)))
121 return NULL;
122 }
123
124 if (!c->namereg)
125 c->namereg = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
126
127 if ((e = pa_hashmap_get(c->namereg, name)) && fail) {
128 pa_xfree(n);
129 return NULL;
130 }
131
132 if (e) {
133 unsigned i;
134 size_t l = strlen(name);
135 char *k;
136
137 if (l+4 > PA_NAME_MAX) {
138 pa_xfree(n);
139 return NULL;
140 }
141
142 k = pa_xnew(char, l+4);
143
144 for (i = 2; i <= 99; i++) {
145 snprintf(k, l+4, "%s.%u", name, i);
146
147 if (!(e = pa_hashmap_get(c->namereg, k)))
148 break;
149 }
150
151 if (e) {
152 pa_xfree(n);
153 pa_xfree(k);
154 return NULL;
155 }
156
157 pa_xfree(n);
158 n = k;
159 }
160
161 e = pa_xnew(struct namereg_entry, 1);
162 e->type = type;
163 e->name = n ? n : pa_xstrdup(name);
164 e->data = data;
165
166 r = pa_hashmap_put(c->namereg, e->name, e);
167 assert (r >= 0);
168
169 return e->name;
170 }
171
172 void pa_namereg_unregister(pa_core *c, const char *name) {
173 struct namereg_entry *e;
174
175 assert(c);
176 assert(name);
177
178 e = pa_hashmap_remove(c->namereg, name);
179 assert(e);
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, int autoload) {
186 struct namereg_entry *e;
187 uint32_t idx;
188 assert(c);
189
190 if (!name) {
191
192 if (type == PA_NAMEREG_SOURCE)
193 name = pa_namereg_get_default_source_name(c);
194 else if (type == PA_NAMEREG_SINK)
195 name = pa_namereg_get_default_sink_name(c);
196
197 } else if (strcmp(name, "@DEFAULT_SINK@") == 0) {
198 if (type == PA_NAMEREG_SINK)
199 name = pa_namereg_get_default_sink_name(c);
200
201 } else if (strcmp(name, "@DEFAULT_SOURCE@") == 0) {
202 if (type == PA_NAMEREG_SOURCE)
203 name = pa_namereg_get_default_source_name(c);
204
205 } else if (strcmp(name, "@DEFAULT_MONITOR@") == 0) {
206 if (type == PA_NAMEREG_SOURCE) {
207 pa_sink *k;
208
209 if ((k = pa_namereg_get(c, NULL, PA_NAMEREG_SINK, autoload)))
210 return k->monitor_source;
211 }
212 } else if (*name == '@')
213 name = NULL;
214
215 if (!name)
216 return NULL;
217
218 if (c->namereg && (e = pa_hashmap_get(c->namereg, name)))
219 if (e->type == type)
220 return e->data;
221
222 if (pa_atou(name, &idx) < 0) {
223
224 if (autoload) {
225 pa_autoload_request(c, name, type);
226
227 if (c->namereg && (e = pa_hashmap_get(c->namereg, name)))
228 if (e->type == type)
229 return e->data;
230 }
231
232 return NULL;
233 }
234
235 if (type == PA_NAMEREG_SINK)
236 return pa_idxset_get_by_index(c->sinks, idx);
237 else if (type == PA_NAMEREG_SOURCE)
238 return pa_idxset_get_by_index(c->sources, idx);
239 else if (type == PA_NAMEREG_SAMPLE && c->scache)
240 return pa_idxset_get_by_index(c->scache, idx);
241
242 return NULL;
243 }
244
245 int pa_namereg_set_default(pa_core*c, const char *name, pa_namereg_type_t type) {
246 char **s;
247
248 assert(c);
249 assert(type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE);
250
251 s = type == PA_NAMEREG_SINK ? &c->default_sink_name : &c->default_source_name;
252
253 if (!name && !*s)
254 return 0;
255
256 if (name && *s && !strcmp(name, *s))
257 return 0;
258
259 if (!is_valid_name(name))
260 return -1;
261
262 pa_xfree(*s);
263 *s = pa_xstrdup(name);
264 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
265
266 return 0;
267 }
268
269 const char *pa_namereg_get_default_sink_name(pa_core *c) {
270 pa_sink *s;
271
272 assert(c);
273
274 if (c->default_sink_name)
275 return c->default_sink_name;
276
277 if ((s = pa_idxset_first(c->sinks, NULL)))
278 pa_namereg_set_default(c, s->name, PA_NAMEREG_SINK);
279
280 return c->default_sink_name;
281 }
282
283 const char *pa_namereg_get_default_source_name(pa_core *c) {
284 pa_source *s;
285 uint32_t idx;
286
287 assert(c);
288
289 if (c->default_source_name)
290 return c->default_source_name;
291
292 for (s = pa_idxset_first(c->sources, &idx); s; s = pa_idxset_next(c->sources, &idx))
293 if (!s->monitor_of) {
294 pa_namereg_set_default(c, s->name, PA_NAMEREG_SOURCE);
295 break;
296 }
297
298 if (!c->default_source_name)
299 if ((s = pa_idxset_first(c->sources, NULL)))
300 pa_namereg_set_default(c, s->name, PA_NAMEREG_SOURCE);
301
302 return c->default_source_name;
303 }