Merge commit 'origin/master-tx'
[profile/ivi/pulseaudio-panda.git] / 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     return e->name;
153 }
154
155 void pa_namereg_unregister(pa_core *c, const char *name) {
156     struct namereg_entry *e;
157
158     pa_assert(c);
159     pa_assert(name);
160
161     pa_assert_se(e = pa_hashmap_remove(c->namereg, name));
162
163     if (c->default_sink == e->data)
164         pa_namereg_set_default_sink(c, NULL);
165     else if (c->default_source == e->data)
166         pa_namereg_set_default_source(c, NULL);
167
168     pa_xfree(e->name);
169     pa_xfree(e);
170 }
171
172 void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) {
173     struct namereg_entry *e;
174     uint32_t idx;
175     pa_assert(c);
176
177     if (type == PA_NAMEREG_SOURCE && (!name || pa_streq(name, "@DEFAULT_SOURCE@"))) {
178         pa_source *s;
179
180         if ((s = pa_namereg_get_default_source(c)))
181             return s;
182
183     } else if (type == PA_NAMEREG_SINK && (!name || pa_streq(name, "@DEFAULT_SINK@"))) {
184         pa_sink *s;
185
186         if ((s = pa_namereg_get_default_sink(c)))
187             return s;
188
189     } else if (type == PA_NAMEREG_SOURCE && name && pa_streq(name, "@DEFAULT_MONITOR@")) {
190         pa_sink *s;
191
192         if ((s = pa_namereg_get(c, NULL, PA_NAMEREG_SINK)))
193             return s->monitor_source;
194
195     }
196
197     if (!name)
198         return NULL;
199
200     if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE || type == PA_NAMEREG_CARD) &&
201         !pa_namereg_is_valid_name(name))
202         return NULL;
203
204     if ((e = pa_hashmap_get(c->namereg, name)))
205         if (e->type == type)
206             return e->data;
207
208     if (pa_atou(name, &idx) < 0)
209         return NULL;
210
211     if (type == PA_NAMEREG_SINK)
212         return pa_idxset_get_by_index(c->sinks, idx);
213     else if (type == PA_NAMEREG_SOURCE)
214         return pa_idxset_get_by_index(c->sources, idx);
215     else if (type == PA_NAMEREG_SAMPLE && c->scache)
216         return pa_idxset_get_by_index(c->scache, idx);
217     else if (type == PA_NAMEREG_CARD)
218         return pa_idxset_get_by_index(c->cards, idx);
219
220     return NULL;
221 }
222
223 pa_sink* pa_namereg_set_default_sink(pa_core*c, pa_sink *s) {
224     pa_assert(c);
225
226     if (c->default_sink != s) {
227         c->default_sink = s;
228         pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
229     }
230
231     return s;
232 }
233
234 pa_source* pa_namereg_set_default_source(pa_core*c, pa_source *s) {
235     pa_assert(c);
236
237     if (c->default_source != s) {
238         c->default_source = s;
239         pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
240     }
241
242     return s;
243 }
244
245 pa_sink *pa_namereg_get_default_sink(pa_core *c) {
246     pa_sink *s;
247
248     pa_assert(c);
249
250     if (c->default_sink)
251         return c->default_sink;
252
253     if ((s = pa_idxset_first(c->sinks, NULL)))
254         return pa_namereg_set_default_sink(c, s);
255
256     return NULL;
257 }
258
259 pa_source *pa_namereg_get_default_source(pa_core *c) {
260     pa_source *s;
261     uint32_t idx;
262
263     pa_assert(c);
264
265     if (c->default_source)
266         return c->default_source;
267
268     for (s = PA_SOURCE(pa_idxset_first(c->sources, &idx)); s; s = PA_SOURCE(pa_idxset_next(c->sources, &idx)))
269         if (!s->monitor_of)
270             return pa_namereg_set_default_source(c, s);
271
272     if ((s = pa_idxset_first(c->sources, NULL)))
273         return pa_namereg_set_default_source(c, s);
274
275     return NULL;
276 }