4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/hashmap.h>
36 #include <pulsecore/idxset.h>
37 #include <pulsecore/sample-util.h>
38 #include <pulsecore/namereg.h>
39 #include <pulsecore/sink.h>
40 #include <pulsecore/source.h>
41 #include <pulsecore/core-util.h>
49 static int add_key_value(pa_hashmap *map, char *key, char *value, const char* const valid_keys[]) {
51 assert(map && key && value);
55 for (v = valid_keys; *v; v++)
56 if (strcmp(*v, key) == 0)
66 e = pa_xmalloc(sizeof(struct entry));
69 pa_hashmap_put(map, key, e);
73 pa_modargs *pa_modargs_new(const char *args, const char* const* valid_keys) {
74 pa_hashmap *map = NULL;
76 map = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
80 enum { WHITESPACE, KEY, VALUE_START, VALUE_SIMPLE, VALUE_DOUBLE_QUOTES, VALUE_TICKS } state;
81 const char *p, *key, *value;
82 size_t key_len = 0, value_len = 0;
86 for (p = args; *p; p++) {
91 else if (!isspace(*p)) {
108 } else if (*p == '"') {
109 state = VALUE_DOUBLE_QUOTES;
112 } else if (isspace(*p)) {
113 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys) < 0)
117 state = VALUE_SIMPLE;
124 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
130 case VALUE_DOUBLE_QUOTES:
132 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
140 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
149 if (state == VALUE_START) {
150 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys) < 0)
152 } else if (state == VALUE_SIMPLE) {
153 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(value), valid_keys) < 0)
155 } else if (state != WHITESPACE)
159 return (pa_modargs*) map;
164 pa_modargs_free((pa_modargs*) map);
170 static void free_func(void *p, PA_GCC_UNUSED void*userdata) {
178 void pa_modargs_free(pa_modargs*ma) {
179 pa_hashmap *map = (pa_hashmap*) ma;
180 pa_hashmap_free(map, free_func, NULL);
183 const char *pa_modargs_get_value(pa_modargs *ma, const char *key, const char *def) {
184 pa_hashmap *map = (pa_hashmap*) ma;
187 if (!(e = pa_hashmap_get(map, key)))
193 int pa_modargs_get_value_u32(pa_modargs *ma, const char *key, uint32_t *value) {
195 assert(ma && key && value);
197 if (!(v = pa_modargs_get_value(ma, key, NULL)))
200 if (pa_atou(v, value) < 0)
206 int pa_modargs_get_value_s32(pa_modargs *ma, const char *key, int32_t *value) {
208 assert(ma && key && value);
210 if (!(v = pa_modargs_get_value(ma, key, NULL)))
213 if (pa_atoi(v, value) < 0)
219 int pa_modargs_get_value_boolean(pa_modargs *ma, const char *key, int *value) {
222 assert(ma && key && value);
224 if (!(v = pa_modargs_get_value(ma, key, NULL)))
230 if ((r = pa_parse_boolean(v)) < 0)
237 int pa_modargs_get_sample_spec(pa_modargs *ma, pa_sample_spec *rss) {
246 if ((pa_modargs_get_value_u32(ma, "rate", &ss.rate)) < 0)
249 channels = ss.channels;
250 if ((pa_modargs_get_value_u32(ma, "channels", &channels)) < 0)
252 ss.channels = (uint8_t) channels;
254 if ((format = pa_modargs_get_value(ma, "format", NULL)))
255 if ((ss.format = pa_parse_sample_format(format)) < 0)
258 if (!pa_sample_spec_valid(&ss))
266 int pa_modargs_get_channel_map(pa_modargs *ma, pa_channel_map *rmap) {
275 if ((cm = pa_modargs_get_value(ma, "channel_map", NULL)))
276 if (!pa_channel_map_parse(&map, cm))
279 if (!pa_channel_map_valid(&map))
286 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) {
296 if (pa_modargs_get_sample_spec(ma, &ss) < 0)
299 if (!pa_channel_map_init_auto(&map, ss.channels, def))
302 if (pa_modargs_get_channel_map(ma, &map) < 0)
305 if (map.channels != ss.channels)