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