]> code.delx.au - pulseaudio/blob - polyp/namereg.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / namereg.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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 polypaudio 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 polypaudio; 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 "namereg.h"
33 #include "autoload.h"
34 #include "source.h"
35 #include "sink.h"
36 #include "xmalloc.h"
37 #include "subscribe.h"
38
39 struct namereg_entry {
40 enum pa_namereg_type type;
41 char *name;
42 void *data;
43 };
44
45 void pa_namereg_free(struct pa_core *c) {
46 assert(c);
47 if (!c->namereg)
48 return;
49 assert(pa_hashmap_ncontents(c->namereg) == 0);
50 pa_hashmap_free(c->namereg, NULL, NULL);
51 }
52
53 const char *pa_namereg_register(struct pa_core *c, const char *name, enum pa_namereg_type type, void *data, int fail) {
54 struct namereg_entry *e;
55 char *n = NULL;
56 int r;
57
58 assert(c && name && data);
59
60 if (!c->namereg) {
61 c->namereg = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
62 assert(c->namereg);
63 }
64
65 if ((e = pa_hashmap_get(c->namereg, name)) && fail)
66 return NULL;
67
68 if (!e)
69 n = pa_xstrdup(name);
70 else {
71 unsigned i;
72 size_t l = strlen(name);
73 n = pa_xmalloc(l+3);
74
75 for (i = 1; i <= 99; i++) {
76 snprintf(n, l+2, "%s%u", name, i);
77
78 if (!(e = pa_hashmap_get(c->namereg, n)))
79 break;
80 }
81
82 if (e) {
83 pa_xfree(n);
84 return NULL;
85 }
86 }
87
88 assert(n);
89 e = pa_xmalloc(sizeof(struct namereg_entry));
90 e->type = type;
91 e->name = n;
92 e->data = data;
93
94 r = pa_hashmap_put(c->namereg, e->name, e);
95 assert (r >= 0);
96
97 return e->name;
98
99 }
100
101 void pa_namereg_unregister(struct pa_core *c, const char *name) {
102 struct namereg_entry *e;
103 assert(c && name);
104
105 e = pa_hashmap_remove(c->namereg, name);
106 assert(e);
107
108 pa_xfree(e->name);
109 pa_xfree(e);
110 }
111
112 void* pa_namereg_get(struct pa_core *c, const char *name, enum pa_namereg_type type, int autoload) {
113 struct namereg_entry *e;
114 uint32_t index;
115 char *x = NULL;
116 void *d = NULL;
117 assert(c);
118
119 if (!name) {
120 if (type == PA_NAMEREG_SOURCE)
121 name = pa_namereg_get_default_source_name(c);
122 else if (type == PA_NAMEREG_SINK)
123 name = pa_namereg_get_default_sink_name(c);
124 }
125
126 if (!name)
127 return NULL;
128
129 if (c->namereg && (e = pa_hashmap_get(c->namereg, name)))
130 if (e->type == e->type)
131 return e->data;
132
133 index = (uint32_t) strtol(name, &x, 0);
134
135 if (!x || *x != 0) {
136
137 if (autoload) {
138 pa_autoload_request(c, name, type);
139
140 if (c->namereg && (e = pa_hashmap_get(c->namereg, name)))
141 if (e->type == e->type)
142 return e->data;
143 }
144
145 return NULL;
146 }
147
148 if (type == PA_NAMEREG_SINK)
149 d = pa_idxset_get_by_index(c->sinks, index);
150 else if (type == PA_NAMEREG_SOURCE)
151 d = pa_idxset_get_by_index(c->sources, index);
152 else if (type == PA_NAMEREG_SAMPLE && c->scache)
153 d = pa_idxset_get_by_index(c->scache, index);
154
155 return d;
156 }
157
158 void pa_namereg_set_default(struct pa_core*c, const char *name, enum pa_namereg_type type) {
159 char **s;
160 assert(c && (type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE));
161
162 s = type == PA_NAMEREG_SINK ? &c->default_sink_name : &c->default_source_name;
163 assert(s);
164
165 if (!name && !*s)
166 return;
167
168 if (name && *s && !strcmp(name, *s))
169 return;
170
171 pa_xfree(*s);
172 *s = pa_xstrdup(name);
173 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
174 }
175
176 const char *pa_namereg_get_default_sink_name(struct pa_core *c) {
177 struct pa_sink *s;
178 assert(c);
179
180 if (c->default_sink_name)
181 return c->default_sink_name;
182
183 if ((s = pa_idxset_first(c->sinks, NULL)))
184 pa_namereg_set_default(c, s->name, PA_NAMEREG_SINK);
185
186 if (c->default_sink_name)
187 return c->default_sink_name;
188
189 return NULL;
190 }
191
192 const char *pa_namereg_get_default_source_name(struct pa_core *c) {
193 struct pa_source *s;
194 uint32_t index;
195
196 assert(c);
197
198 if (c->default_source_name)
199 return c->default_source_name;
200
201 for (s = pa_idxset_first(c->sources, &index); s; s = pa_idxset_next(c->sources, &index))
202 if (!s->monitor_of) {
203 pa_namereg_set_default(c, s->name, PA_NAMEREG_SOURCE);
204 break;
205 }
206
207 if (!c->default_source_name)
208 if ((s = pa_idxset_first(c->sources, NULL)))
209 pa_namereg_set_default(c, s->name, PA_NAMEREG_SOURCE);
210
211 if (c->default_source_name)
212 return c->default_source_name;
213
214 return NULL;
215 }