]> code.delx.au - pulseaudio/blob - src/pulsecore/namereg.c
Finish the Core dbus interface.
[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 = pa_idxset_first(c->sinks, &idx);
171
172 if (new_default == e->data)
173 new_default = pa_idxset_next(c->sinks, &idx);
174
175 pa_namereg_set_default_sink(c, new_default);
176
177 } else if (c->default_source == e->data) {
178 pa_source *new_default;
179
180 for (new_default = pa_idxset_first(c->sources, &idx); new_default; new_default = pa_idxset_next(c->sources, &idx)) {
181 if (new_default != e->data && !new_default->monitor_of)
182 break;
183 }
184
185 if (!new_default) {
186 new_default = pa_idxset_first(c->sources, &idx);
187
188 if (new_default == e->data)
189 new_default = pa_idxset_next(c->sources, &idx);
190 }
191
192 pa_namereg_set_default_source(c, new_default);
193 }
194
195 pa_xfree(e->name);
196 pa_xfree(e);
197 }
198
199 void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) {
200 struct namereg_entry *e;
201 uint32_t idx;
202 pa_assert(c);
203
204 if (type == PA_NAMEREG_SOURCE && (!name || pa_streq(name, "@DEFAULT_SOURCE@"))) {
205 pa_source *s;
206
207 if ((s = pa_namereg_get_default_source(c)))
208 return s;
209
210 } else if (type == PA_NAMEREG_SINK && (!name || pa_streq(name, "@DEFAULT_SINK@"))) {
211 pa_sink *s;
212
213 if ((s = pa_namereg_get_default_sink(c)))
214 return s;
215
216 } else if (type == PA_NAMEREG_SOURCE && name && pa_streq(name, "@DEFAULT_MONITOR@")) {
217 pa_sink *s;
218
219 if ((s = pa_namereg_get(c, NULL, PA_NAMEREG_SINK)))
220 return s->monitor_source;
221 }
222
223 if (!name)
224 return NULL;
225
226 if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE || type == PA_NAMEREG_CARD) &&
227 !pa_namereg_is_valid_name(name))
228 return NULL;
229
230 if ((e = pa_hashmap_get(c->namereg, name)))
231 if (e->type == type)
232 return e->data;
233
234 if (pa_atou(name, &idx) < 0)
235 return NULL;
236
237 if (type == PA_NAMEREG_SINK)
238 return pa_idxset_get_by_index(c->sinks, idx);
239 else if (type == PA_NAMEREG_SOURCE)
240 return pa_idxset_get_by_index(c->sources, idx);
241 else if (type == PA_NAMEREG_SAMPLE && c->scache)
242 return pa_idxset_get_by_index(c->scache, idx);
243 else if (type == PA_NAMEREG_CARD)
244 return pa_idxset_get_by_index(c->cards, idx);
245
246 return NULL;
247 }
248
249 pa_sink* pa_namereg_set_default_sink(pa_core*c, pa_sink *s) {
250 pa_assert(c);
251
252 if (c->default_sink != s) {
253 c->default_sink = s;
254 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
255 }
256
257 return s;
258 }
259
260 pa_source* pa_namereg_set_default_source(pa_core*c, pa_source *s) {
261 pa_assert(c);
262
263 if (c->default_source != s) {
264 c->default_source = s;
265 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
266 }
267
268 return s;
269 }
270
271 /* XXX: After removing old functionality, has this function become useless? */
272 pa_sink *pa_namereg_get_default_sink(pa_core *c) {
273 pa_assert(c);
274
275 return c->default_sink;
276 }
277
278 /* XXX: After removing old functionality, has this function become useless? */
279 pa_source *pa_namereg_get_default_source(pa_core *c) {
280 pa_assert(c);
281
282 return c->default_source;
283 }