4 This file is part of polypaudio.
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.
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.
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
32 #include <polyp/xmalloc.h>
34 #include <polypcore/autoload.h>
35 #include <polypcore/source.h>
36 #include <polypcore/sink.h>
37 #include <polypcore/core-subscribe.h>
38 #include <polypcore/core-util.h>
42 struct namereg_entry
{
43 pa_namereg_type_t type
;
48 void pa_namereg_free(pa_core
*c
) {
52 assert(pa_hashmap_size(c
->namereg
) == 0);
53 pa_hashmap_free(c
->namereg
, NULL
, NULL
);
56 const char *pa_namereg_register(pa_core
*c
, const char *name
, pa_namereg_type_t type
, void *data
, int fail
) {
57 struct namereg_entry
*e
;
61 assert(c
&& name
&& data
);
64 c
->namereg
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
68 if ((e
= pa_hashmap_get(c
->namereg
, name
)) && fail
)
75 size_t l
= strlen(name
);
78 for (i
= 1; i
<= 99; i
++) {
79 snprintf(n
, l
+2, "%s%u", name
, i
);
81 if (!(e
= pa_hashmap_get(c
->namereg
, n
)))
92 e
= pa_xmalloc(sizeof(struct namereg_entry
));
97 r
= pa_hashmap_put(c
->namereg
, e
->name
, e
);
104 void pa_namereg_unregister(pa_core
*c
, const char *name
) {
105 struct namereg_entry
*e
;
108 e
= pa_hashmap_remove(c
->namereg
, name
);
115 void* pa_namereg_get(pa_core
*c
, const char *name
, pa_namereg_type_t type
, int autoload
) {
116 struct namereg_entry
*e
;
121 if (type
== PA_NAMEREG_SOURCE
)
122 name
= pa_namereg_get_default_source_name(c
);
123 else if (type
== PA_NAMEREG_SINK
)
124 name
= pa_namereg_get_default_sink_name(c
);
130 if (c
->namereg
&& (e
= pa_hashmap_get(c
->namereg
, name
)))
134 if (pa_atou(name
, &idx
) < 0) {
137 pa_autoload_request(c
, name
, type
);
139 if (c
->namereg
&& (e
= pa_hashmap_get(c
->namereg
, name
)))
147 if (type
== PA_NAMEREG_SINK
)
148 return pa_idxset_get_by_index(c
->sinks
, idx
);
149 else if (type
== PA_NAMEREG_SOURCE
)
150 return pa_idxset_get_by_index(c
->sources
, idx
);
151 else if (type
== PA_NAMEREG_SAMPLE
&& c
->scache
)
152 return pa_idxset_get_by_index(c
->scache
, idx
);
157 void pa_namereg_set_default(pa_core
*c
, const char *name
, pa_namereg_type_t type
) {
159 assert(c
&& (type
== PA_NAMEREG_SINK
|| type
== PA_NAMEREG_SOURCE
));
161 s
= type
== PA_NAMEREG_SINK
? &c
->default_sink_name
: &c
->default_source_name
;
167 if (name
&& *s
&& !strcmp(name
, *s
))
171 *s
= pa_xstrdup(name
);
172 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SERVER
|PA_SUBSCRIPTION_EVENT_CHANGE
, PA_INVALID_INDEX
);
175 const char *pa_namereg_get_default_sink_name(pa_core
*c
) {
179 if (c
->default_sink_name
)
180 return c
->default_sink_name
;
182 if ((s
= pa_idxset_first(c
->sinks
, NULL
)))
183 pa_namereg_set_default(c
, s
->name
, PA_NAMEREG_SINK
);
185 if (c
->default_sink_name
)
186 return c
->default_sink_name
;
191 const char *pa_namereg_get_default_source_name(pa_core
*c
) {
197 if (c
->default_source_name
)
198 return c
->default_source_name
;
200 for (s
= pa_idxset_first(c
->sources
, &idx
); s
; s
= pa_idxset_next(c
->sources
, &idx
))
201 if (!s
->monitor_of
) {
202 pa_namereg_set_default(c
, s
->name
, PA_NAMEREG_SOURCE
);
206 if (!c
->default_source_name
)
207 if ((s
= pa_idxset_first(c
->sources
, NULL
)))
208 pa_namereg_set_default(c
, s
->name
, PA_NAMEREG_SOURCE
);
210 if (c
->default_source_name
)
211 return c
->default_source_name
;