2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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 of the License,
9 or (at your option) any later version.
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.
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
30 #include <pulse/xmalloc.h>
32 #include <pulsecore/hashmap.h>
33 #include <pulsecore/idxset.h>
34 #include <pulsecore/sample-util.h>
35 #include <pulsecore/namereg.h>
36 #include <pulsecore/sink.h>
37 #include <pulsecore/source.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/macro.h>
47 static int add_key_value(pa_hashmap
*map
, char *key
, char *value
, const char* const valid_keys
[]) {
54 if (pa_hashmap_get(map
, key
)) {
62 for (v
= valid_keys
; *v
; v
++)
63 if (strcmp(*v
, key
) == 0)
73 e
= pa_xnew(struct entry
, 1);
76 pa_hashmap_put(map
, key
, e
);
81 pa_modargs
*pa_modargs_new(const char *args
, const char* const* valid_keys
) {
91 const char *p
, *key
= NULL
, *value
= NULL
;
92 size_t key_len
= 0, value_len
= 0;
95 map
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
98 return (pa_modargs
*) map
;
102 for (p
= args
; *p
; p
++) {
108 else if (!isspace(*p
)) {
118 else if (isspace(*p
))
129 } else if (*p
== '"') {
130 state
= VALUE_DOUBLE_QUOTES
;
133 } else if (isspace(*p
)) {
134 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
138 state
= VALUE_SIMPLE
;
146 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
153 case VALUE_DOUBLE_QUOTES
:
155 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
164 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
173 if (state
== VALUE_START
) {
174 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
176 } else if (state
== VALUE_SIMPLE
) {
177 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(value
), valid_keys
) < 0)
179 } else if (state
!= WHITESPACE
)
182 return (pa_modargs
*) map
;
186 pa_modargs_free((pa_modargs
*) map
);
191 static void free_func(void *p
, void*userdata
) {
200 void pa_modargs_free(pa_modargs
*ma
) {
201 pa_hashmap
*map
= (pa_hashmap
*) ma
;
202 pa_hashmap_free(map
, free_func
, NULL
);
205 const char *pa_modargs_get_value(pa_modargs
*ma
, const char *key
, const char *def
) {
206 pa_hashmap
*map
= (pa_hashmap
*) ma
;
209 if (!(e
= pa_hashmap_get(map
, key
)))
215 int pa_modargs_get_value_u32(pa_modargs
*ma
, const char *key
, uint32_t *value
) {
222 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
225 if (pa_atou(v
, value
) < 0)
231 int pa_modargs_get_value_s32(pa_modargs
*ma
, const char *key
, int32_t *value
) {
238 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
241 if (pa_atoi(v
, value
) < 0)
247 int pa_modargs_get_value_boolean(pa_modargs
*ma
, const char *key
, pa_bool_t
*value
) {
255 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
261 if ((r
= pa_parse_boolean(v
)) < 0)
268 int pa_modargs_get_sample_spec(pa_modargs
*ma
, pa_sample_spec
*rss
) {
277 if ((pa_modargs_get_value_u32(ma
, "rate", &ss
.rate
)) < 0)
280 channels
= ss
.channels
;
281 if ((pa_modargs_get_value_u32(ma
, "channels", &channels
)) < 0)
283 ss
.channels
= (uint8_t) channels
;
285 if ((format
= pa_modargs_get_value(ma
, "format", NULL
)))
286 if ((ss
.format
= pa_parse_sample_format(format
)) < 0)
289 if (!pa_sample_spec_valid(&ss
))
297 int pa_modargs_get_channel_map(pa_modargs
*ma
, const char *name
, pa_channel_map
*rmap
) {
306 if ((cm
= pa_modargs_get_value(ma
, name
? name
: "channel_map", NULL
)))
307 if (!pa_channel_map_parse(&map
, cm
))
310 if (!pa_channel_map_valid(&map
))
317 int pa_modargs_get_sample_spec_and_channel_map(pa_modargs
*ma
, pa_sample_spec
*rss
, pa_channel_map
*rmap
, pa_channel_map_def_t def
) {
327 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0)
330 pa_channel_map_init_extend(&map
, ss
.channels
, def
);
332 if (pa_modargs_get_channel_map(ma
, NULL
, &map
) < 0)
335 if (map
.channels
!= ss
.channels
)