]> code.delx.au - pulseaudio/blob - src/pulsecore/namereg.c
cleanup idxset.[ch] a little: define proper types for the hash/compare funcs, do...
[pulseaudio] / src / pulsecore / namereg.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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 <assert.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/autoload.h>
35 #include <pulsecore/source.h>
36 #include <pulsecore/sink.h>
37 #include <pulsecore/core-subscribe.h>
38 #include <pulsecore/core-util.h>
39
40 #include "namereg.h"
41
42 struct namereg_entry {
43 pa_namereg_type_t type;
44 char *name;
45 void *data;
46 };
47
48 static int is_valid_char(char c) {
49 return
50 (c >= 'a' && c <= 'z') ||
51 (c >= 'A' && c <= 'Z') ||
52 (c >= '0' && c <= '9') ||
53 c == '.' ||
54 c == '_';
55 }
56
57 static int is_valid_name(const char *name) {
58 const char *c;
59
60 if (*name == 0)
61 return 0;
62
63 for (c = name; *c && (c-name < PA_NAME_MAX); c++)
64 if (!is_valid_char(*c))
65 return 0;
66
67 if (*c)
68 return 0;
69
70 return 1;
71 }
72
73 static char* cleanup_name(const char *name) {
74 const char *a;
75 char *b, *n;
76
77 if (*name == 0)
78 return NULL;
79
80 n = pa_xnew(char, strlen(name)+1);
81
82 for (a = name, b = n; *a && (a-name < PA_NAME_MAX); a++, b++)
83 *b = is_valid_char(*a) ? *a : '_';
84
85 *b = 0;
86
87 return n;
88 }
89
90 void pa_namereg_free(pa_core *c) {
91 assert(c);
92
93 if (!c->namereg)
94 return;
95
96 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, int fail) {
101 struct namereg_entry *e;
102 char *n = NULL;
103 int r;
104
105 assert(c);
106 assert(name);
107 assert(data);
108
109 if (!*name)
110 return NULL;
111
112 if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE) &&
113 !is_valid_name(name) ) {
114
115 if (fail)
116 return NULL;
117
118 if (!(name = n = cleanup_name(name)))
119 return NULL;
120 }
121
122 if (!c->namereg)
123 c->namereg = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
124
125 if ((e = pa_hashmap_get(c->namereg, name)) && fail) {
126 pa_xfree(n);
127 return NULL;
128 }
129
130 if (e) {
131 unsigned i;
132 size_t l = strlen(name);
133 char *k;
134
135 if (l+4 > PA_NAME_MAX) {
136 pa_xfree(n);
137 return NULL;
138 }
139
140 k = pa_xnew(char, l+4);
141
142 for (i = 2; i <= 99; i++) {
143 snprintf(k, l+4, "%s.%u", name, i);
144
145 if (!(e = pa_hashmap_get(c->namereg, k)))
146 break;
147 }
148
149 if (e) {
150 pa_xfree(n);
151 pa_xfree(k);
152 return NULL;
153 }
154
155 pa_xfree(n);
156 n = k;
157 }
158
159 e = pa_xnew(struct namereg_entry, 1);
160 e->type = type;
161 e->name = n ? n : pa_xstrdup(name);
162 e->data = data;
163
164 r = pa_hashmap_put(c->namereg, e->name, e);
165 assert (r >= 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 assert(c);
174 assert(name);
175
176 e = pa_hashmap_remove(c->namereg, name);
177 assert(e);
178
179 pa_xfree(e->name);
180 pa_xfree(e);
181 }
182
183 void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type, int autoload) {
184 struct namereg_entry *e;
185 uint32_t idx;
186 assert(c);
187
188 if (!name) {
189
190 if (type == PA_NAMEREG_SOURCE)
191 name = pa_namereg_get_default_source_name(c);
192 else if (type == PA_NAMEREG_SINK)
193 name = pa_namereg_get_default_sink_name(c);
194
195 } else if (strcmp(name, "@DEFAULT_SINK@") == 0) {
196 if (type == PA_NAMEREG_SINK)
197 name = pa_namereg_get_default_sink_name(c);
198
199 } else if (strcmp(name, "@DEFAULT_SOURCE@") == 0) {
200 if (type == PA_NAMEREG_SOURCE)
201 name = pa_namereg_get_default_source_name(c);
202
203 } else if (strcmp(name, "@DEFAULT_MONITOR@") == 0) {
204 if (type == PA_NAMEREG_SOURCE) {
205 pa_sink *k;
206
207 if ((k = pa_namereg_get(c, NULL, PA_NAMEREG_SINK, autoload)))
208 return k->monitor_source;
209 }
210 } else if (*name == '@')
211 name = NULL;
212
213 if (!name)
214 return NULL;
215
216 if (c->namereg && (e = pa_hashmap_get(c->namereg, name)))
217 if (e->type == type)
218 return e->data;
219
220 if (pa_atou(name, &idx) < 0) {
221
222 if (autoload) {
223 pa_autoload_request(c, name, type);
224
225 if (c->namereg && (e = pa_hashmap_get(c->namereg, name)))
226 if (e->type == type)
227 return e->data;
228 }
229
230 return NULL;
231 }
232
233 if (type == PA_NAMEREG_SINK)
234 return pa_idxset_get_by_index(c->sinks, idx);
235 else if (type == PA_NAMEREG_SOURCE)
236 return pa_idxset_get_by_index(c->sources, idx);
237 else if (type == PA_NAMEREG_SAMPLE && c->scache)
238 return pa_idxset_get_by_index(c->scache, idx);
239
240 return NULL;
241 }
242
243 int pa_namereg_set_default(pa_core*c, const char *name, pa_namereg_type_t type) {
244 char **s;
245
246 assert(c);
247 assert(type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE);
248
249 s = type == PA_NAMEREG_SINK ? &c->default_sink_name : &c->default_source_name;
250
251 if (!name && !*s)
252 return 0;
253
254 if (name && *s && !strcmp(name, *s))
255 return 0;
256
257 if (!is_valid_name(name))
258 return -1;
259
260 pa_xfree(*s);
261 *s = pa_xstrdup(name);
262 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
263
264 return 0;
265 }
266
267 const char *pa_namereg_get_default_sink_name(pa_core *c) {
268 pa_sink *s;
269
270 assert(c);
271
272 if (c->default_sink_name)
273 return c->default_sink_name;
274
275 if ((s = pa_idxset_first(c->sinks, NULL)))
276 pa_namereg_set_default(c, s->name, PA_NAMEREG_SINK);
277
278 return c->default_sink_name;
279 }
280
281 const char *pa_namereg_get_default_source_name(pa_core *c) {
282 pa_source *s;
283 uint32_t idx;
284
285 assert(c);
286
287 if (c->default_source_name)
288 return c->default_source_name;
289
290 for (s = pa_idxset_first(c->sources, &idx); s; s = pa_idxset_next(c->sources, &idx))
291 if (!s->monitor_of) {
292 pa_namereg_set_default(c, s->name, PA_NAMEREG_SOURCE);
293 break;
294 }
295
296 if (!c->default_source_name)
297 if ((s = pa_idxset_first(c->sources, NULL)))
298 pa_namereg_set_default(c, s->name, PA_NAMEREG_SOURCE);
299
300 return c->default_source_name;
301 }