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
) {
82 pa_hashmap
*map
= NULL
;
84 map
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
96 const char *p
, *key
, *value
;
97 size_t key_len
= 0, value_len
= 0;
101 for (p
= args
; *p
; p
++) {
106 else if (!isspace(*p
)) {
115 else if (isspace(*p
))
125 } else if (*p
== '"') {
126 state
= VALUE_DOUBLE_QUOTES
;
129 } else if (isspace(*p
)) {
130 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
134 state
= VALUE_SIMPLE
;
141 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
147 case VALUE_DOUBLE_QUOTES
:
149 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
157 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
166 if (state
== VALUE_START
) {
167 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
169 } else if (state
== VALUE_SIMPLE
) {
170 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(value
), valid_keys
) < 0)
172 } else if (state
!= WHITESPACE
)
176 return (pa_modargs
*) map
;
181 pa_modargs_free((pa_modargs
*) map
);
186 static void free_func(void *p
, PA_GCC_UNUSED
void*userdata
) {
195 void pa_modargs_free(pa_modargs
*ma
) {
196 pa_hashmap
*map
= (pa_hashmap
*) ma
;
197 pa_hashmap_free(map
, free_func
, NULL
);
200 const char *pa_modargs_get_value(pa_modargs
*ma
, const char *key
, const char *def
) {
201 pa_hashmap
*map
= (pa_hashmap
*) ma
;
204 if (!(e
= pa_hashmap_get(map
, key
)))
210 int pa_modargs_get_value_u32(pa_modargs
*ma
, const char *key
, uint32_t *value
) {
217 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
220 if (pa_atou(v
, value
) < 0)
226 int pa_modargs_get_value_s32(pa_modargs
*ma
, const char *key
, int32_t *value
) {
233 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
236 if (pa_atoi(v
, value
) < 0)
242 int pa_modargs_get_value_boolean(pa_modargs
*ma
, const char *key
, pa_bool_t
*value
) {
250 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
256 if ((r
= pa_parse_boolean(v
)) < 0)
263 int pa_modargs_get_sample_spec(pa_modargs
*ma
, pa_sample_spec
*rss
) {
272 if ((pa_modargs_get_value_u32(ma
, "rate", &ss
.rate
)) < 0)
275 channels
= ss
.channels
;
276 if ((pa_modargs_get_value_u32(ma
, "channels", &channels
)) < 0)
278 ss
.channels
= (uint8_t) channels
;
280 if ((format
= pa_modargs_get_value(ma
, "format", NULL
)))
281 if ((ss
.format
= pa_parse_sample_format(format
)) < 0)
284 if (!pa_sample_spec_valid(&ss
))
292 int pa_modargs_get_channel_map(pa_modargs
*ma
, const char *name
, pa_channel_map
*rmap
) {
301 if ((cm
= pa_modargs_get_value(ma
, name
? name
: "channel_map", NULL
)))
302 if (!pa_channel_map_parse(&map
, cm
))
305 if (!pa_channel_map_valid(&map
))
312 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
) {
322 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0)
325 pa_channel_map_init_extend(&map
, ss
.channels
, def
);
327 if (pa_modargs_get_channel_map(ma
, NULL
, &map
) < 0)
330 if (map
.channels
!= ss
.channels
)