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 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 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
38 #include "subscribe.h"
40 struct namereg_entry
{
41 enum pa_namereg_type type
;
46 void pa_namereg_free(struct pa_core
*c
) {
50 assert(pa_hashmap_ncontents(c
->namereg
) == 0);
51 pa_hashmap_free(c
->namereg
, NULL
, NULL
);
54 const char *pa_namereg_register(struct pa_core
*c
, const char *name
, enum pa_namereg_type type
, void *data
, int fail
) {
55 struct namereg_entry
*e
;
59 assert(c
&& name
&& data
);
62 c
->namereg
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
66 if ((e
= pa_hashmap_get(c
->namereg
, name
)) && fail
)
73 size_t l
= strlen(name
);
76 for (i
= 1; i
<= 99; i
++) {
77 snprintf(n
, l
+2, "%s%u", name
, i
);
79 if (!(e
= pa_hashmap_get(c
->namereg
, n
)))
90 e
= pa_xmalloc(sizeof(struct namereg_entry
));
95 r
= pa_hashmap_put(c
->namereg
, e
->name
, e
);
102 void pa_namereg_unregister(struct pa_core
*c
, const char *name
) {
103 struct namereg_entry
*e
;
107 e
= pa_hashmap_get(c
->namereg
, name
);
110 r
= pa_hashmap_remove(c
->namereg
, name
);
117 void* pa_namereg_get(struct pa_core
*c
, const char *name
, enum pa_namereg_type type
, int autoload
) {
118 struct namereg_entry
*e
;
125 if (type
== PA_NAMEREG_SOURCE
) {
126 if (!c
->default_source_name
) {
129 for (s
= pa_idxset_first(c
->sources
, &index
); s
; s
= pa_idxset_next(c
->sources
, &index
))
130 if (!s
->monitor_of
) {
131 pa_namereg_set_default(c
, s
->name
, PA_NAMEREG_SOURCE
);
136 name
= c
->default_source_name
;
138 } else if (type
== PA_NAMEREG_SINK
) {
140 if (!c
->default_sink_name
) {
143 if ((s
= pa_idxset_first(c
->sinks
, NULL
)))
144 pa_namereg_set_default(c
, s
->name
, PA_NAMEREG_SINK
);
147 name
= c
->default_sink_name
;
154 if (c
->namereg
&& (e
= pa_hashmap_get(c
->namereg
, name
)))
155 if (e
->type
== e
->type
)
158 index
= (uint32_t) strtol(name
, &x
, 0);
163 pa_autoload_request(c
, name
, type
);
165 if (c
->namereg
&& (e
= pa_hashmap_get(c
->namereg
, name
)))
166 if (e
->type
== e
->type
)
173 if (type
== PA_NAMEREG_SINK
)
174 d
= pa_idxset_get_by_index(c
->sinks
, index
);
175 else if (type
== PA_NAMEREG_SOURCE
)
176 d
= pa_idxset_get_by_index(c
->sources
, index
);
177 else if (type
== PA_NAMEREG_SAMPLE
&& c
->scache
)
178 d
= pa_idxset_get_by_index(c
->scache
, index
);
183 void pa_namereg_set_default(struct pa_core
*c
, const char *name
, enum pa_namereg_type type
) {
185 assert(c
&& (type
== PA_NAMEREG_SINK
|| type
== PA_NAMEREG_SOURCE
));
187 s
= type
== PA_NAMEREG_SINK
? &c
->default_sink_name
: &c
->default_source_name
;
193 if (name
&& *s
&& !strcmp(name
, *s
))
197 *s
= pa_xstrdup(name
);
198 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SERVER
|PA_SUBSCRIPTION_EVENT_CHANGE
, PA_INVALID_INDEX
);