[2477] | 1 | //config.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
| 5 | * |
---|
| 6 | * This file is part of libroar a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroar is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | * NOTE for everyone want's to change something and send patches: |
---|
| 24 | * read README and HACKING! There a addition information on |
---|
| 25 | * the license of this document you need to read before you send |
---|
| 26 | * any patches. |
---|
| 27 | * |
---|
| 28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 29 | * or libpulse*: |
---|
| 30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #include "libroar.h" |
---|
| 36 | |
---|
[2895] | 37 | static struct roar_libroar_config_codec * |
---|
| 38 | roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config); |
---|
| 39 | |
---|
| 40 | |
---|
[2478] | 41 | struct roar_libroar_config * roar_libroar_get_config_ptr(void) { |
---|
| 42 | static struct roar_libroar_config config; |
---|
| 43 | static int inited = 0; |
---|
| 44 | |
---|
| 45 | if ( !inited ) { |
---|
| 46 | memset(&config, 0, sizeof(config)); |
---|
[2567] | 47 | |
---|
| 48 | config.server = NULL; |
---|
| 49 | |
---|
[2478] | 50 | inited++; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | return &config; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | struct roar_libroar_config * roar_libroar_get_config(void) { |
---|
| 57 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
| 58 | static int inited = 0; |
---|
[2884] | 59 | char * next; |
---|
[2478] | 60 | |
---|
| 61 | if ( !inited ) { |
---|
[2480] | 62 | next = getenv("ROAR_OPTIONS"); |
---|
| 63 | |
---|
[2884] | 64 | if ( next != NULL ) { |
---|
| 65 | roar_libroar_config_parse(next, " "); |
---|
[2480] | 66 | } |
---|
| 67 | |
---|
[2478] | 68 | inited++; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | return config; |
---|
| 72 | } |
---|
| 73 | |
---|
[2890] | 74 | #define _P_FP(v) ((int)(atof((v))*256.0)) |
---|
| 75 | #define _P_INT(v) (atoi((v))) |
---|
| 76 | #define _P_BOOL(v) (*(v) == 'y' || *(v) == 'j' || *(v) == 't' || *(v) == '1' ? 1 : 0) |
---|
| 77 | |
---|
| 78 | static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) { |
---|
| 79 | struct roar_libroar_config_codec * codec_cfg; |
---|
| 80 | int codec; |
---|
| 81 | char * codec_str, * option_str, * value_str; |
---|
| 82 | |
---|
[2912] | 83 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
| 84 | |
---|
[2890] | 85 | if ( config == NULL || txt == NULL ) |
---|
| 86 | return -1; |
---|
| 87 | |
---|
[2912] | 88 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
| 89 | |
---|
[2892] | 90 | codec_str = strtok(txt, ":"); |
---|
[2890] | 91 | |
---|
[2892] | 92 | if ( codec_str == NULL ) |
---|
| 93 | return -1; |
---|
| 94 | |
---|
| 95 | option_str = strtok(NULL, ":"); |
---|
[2890] | 96 | |
---|
| 97 | if ( option_str == NULL ) |
---|
| 98 | return -1; |
---|
| 99 | |
---|
[2892] | 100 | value_str = strtok(NULL, ":"); |
---|
[2890] | 101 | |
---|
| 102 | if ( value_str == NULL ) |
---|
| 103 | return -1; |
---|
| 104 | |
---|
[2912] | 105 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
| 106 | |
---|
[2890] | 107 | if ( (codec = roar_str2codec(codec_str)) == -1 ) { |
---|
| 108 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec: %s", codec_str); |
---|
| 109 | return -1; |
---|
| 110 | } |
---|
| 111 | |
---|
[2912] | 112 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec); |
---|
| 113 | |
---|
[2893] | 114 | if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL ) |
---|
[2890] | 115 | return -1; |
---|
| 116 | |
---|
[2912] | 117 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg); |
---|
| 118 | |
---|
[2890] | 119 | if ( !strcmp(option_str, "q") || !strcmp(option_str, "quality") ) { |
---|
| 120 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_Q; |
---|
| 121 | codec_cfg->q = _P_FP(value_str); |
---|
| 122 | } else if ( !strcmp(option_str, "complexity") ) { |
---|
| 123 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY; |
---|
| 124 | codec_cfg->complexity = _P_FP(value_str); |
---|
| 125 | } else if ( !strcmp(option_str, "dtx") ) { |
---|
| 126 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_DTX; |
---|
| 127 | codec_cfg->dtx = _P_BOOL(value_str); |
---|
| 128 | } else if ( !strcmp(option_str, "cc-max") ) { |
---|
| 129 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MAX_CC; |
---|
| 130 | codec_cfg->max_cc = _P_INT(value_str); |
---|
[2926] | 131 | } else if ( !strcmp(option_str, "vbr") ) { |
---|
| 132 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_VBR; |
---|
| 133 | codec_cfg->vbr = _P_BOOL(value_str); |
---|
[2962] | 134 | } else if ( !strcmp(option_str, "mode") ) { |
---|
| 135 | if ( !strcmp(value_str, "nb") ) { |
---|
| 136 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE; |
---|
| 137 | codec_cfg->mode = ROAR_LIBROAR_CONFIG_MODE_NB; |
---|
| 138 | } else if ( !strcmp(value_str, "wb") ) { |
---|
| 139 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE; |
---|
| 140 | codec_cfg->mode = ROAR_LIBROAR_CONFIG_MODE_WB; |
---|
| 141 | } else if ( !strcmp(value_str, "uwb") ) { |
---|
| 142 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE; |
---|
| 143 | codec_cfg->mode = ROAR_LIBROAR_CONFIG_MODE_UWB; |
---|
| 144 | } else { |
---|
| 145 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec mode: %s", value_str); |
---|
| 146 | return -1; |
---|
| 147 | } |
---|
[2890] | 148 | } else { |
---|
[2891] | 149 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str); |
---|
[2892] | 150 | return -1; |
---|
[2890] | 151 | } |
---|
| 152 | |
---|
[2892] | 153 | return 0; |
---|
[2890] | 154 | } |
---|
| 155 | |
---|
[2884] | 156 | int roar_libroar_config_parse(char * txt, char * delm) { |
---|
| 157 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
| 158 | char * k, * v, * next = txt; |
---|
| 159 | |
---|
| 160 | while (next != NULL) { |
---|
| 161 | k = next; |
---|
[2886] | 162 | |
---|
| 163 | if ( delm == NULL ) { |
---|
| 164 | // no delm -> we have only one option |
---|
| 165 | next = NULL; |
---|
| 166 | } else { |
---|
| 167 | next = strstr(next, delm); |
---|
| 168 | } |
---|
| 169 | |
---|
[2884] | 170 | if ( next != NULL ) { |
---|
| 171 | *next = 0; |
---|
| 172 | next++; |
---|
| 173 | } |
---|
| 174 | |
---|
[2888] | 175 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 176 | |
---|
[2887] | 177 | // strip leading spaces: |
---|
| 178 | while ( *k == ' ' ) k++; |
---|
| 179 | |
---|
[2888] | 180 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 181 | |
---|
[2887] | 182 | // strip tailing new lions: |
---|
| 183 | v = strtok(k, "\r\n"); |
---|
| 184 | if ( v != NULL ) { |
---|
| 185 | if ( *v == '\r' || *v == '\n' ) |
---|
| 186 | *v = 0; |
---|
| 187 | } |
---|
| 188 | |
---|
[2888] | 189 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 190 | |
---|
[2887] | 191 | // comments |
---|
| 192 | if ( *k == '#' ) |
---|
| 193 | continue; |
---|
| 194 | |
---|
[2888] | 195 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 196 | |
---|
[2887] | 197 | // empty options: |
---|
| 198 | if ( *k == 0 ) |
---|
| 199 | continue; |
---|
| 200 | |
---|
[2884] | 201 | if ( (v = strstr(k, ":")) != NULL ) { |
---|
| 202 | *v = 0; |
---|
| 203 | v++; |
---|
| 204 | } |
---|
| 205 | |
---|
[2888] | 206 | ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v); |
---|
| 207 | |
---|
[2884] | 208 | if ( !strcmp(k, "workaround") ) { |
---|
| 209 | if ( !strcmp(v, "use-execed") ) { |
---|
| 210 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED; |
---|
| 211 | } else { |
---|
[2888] | 212 | ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v); |
---|
[2884] | 213 | } |
---|
| 214 | } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) { |
---|
| 215 | if ( !strcmp(v, "sysio") ) { |
---|
| 216 | config->warnings.sysio = ROAR_WARNING_ALWAYS; |
---|
| 217 | } else { |
---|
[2888] | 218 | ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v); |
---|
[2884] | 219 | } |
---|
[2890] | 220 | } else if ( !strcmp(k, "codec") ) { |
---|
| 221 | if ( roar_libroar_config_parse_codec(config, v) == -1 ) { |
---|
| 222 | ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option"); |
---|
| 223 | } |
---|
[2896] | 224 | } else if ( !strcmp(k, "set-server") ) { |
---|
[2899] | 225 | if ( roar_libroar_get_server() == NULL ) |
---|
| 226 | roar_libroar_set_server(v); |
---|
[2884] | 227 | } else { |
---|
[2888] | 228 | ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k); |
---|
[2884] | 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | return 0; |
---|
| 233 | } |
---|
| 234 | |
---|
[2886] | 235 | struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) { |
---|
[2893] | 236 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
| 237 | return roar_libroar_config_codec_get_conf(codec, create, config); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | static struct roar_libroar_config_codec * |
---|
| 241 | roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config) { |
---|
[2886] | 242 | int i; |
---|
| 243 | int need_new = 1; |
---|
| 244 | |
---|
[2912] | 245 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
| 246 | |
---|
[2886] | 247 | if ( codec < 0 || create < 0 ) |
---|
| 248 | return NULL; |
---|
| 249 | |
---|
[2912] | 250 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
| 251 | |
---|
[2886] | 252 | if ( config->codecs.num == 0 ) { |
---|
| 253 | // no match case: |
---|
| 254 | if ( !create ) |
---|
| 255 | return NULL; |
---|
| 256 | } else { |
---|
| 257 | for (i = 0; i < config->codecs.num; i++) { |
---|
| 258 | if ( config->codecs.codec[i].codec == codec ) |
---|
| 259 | return &(config->codecs.codec[i]); |
---|
| 260 | if ( config->codecs.codec[i].codec == -1 ) |
---|
| 261 | need_new = 0; |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
[2912] | 265 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
| 266 | |
---|
[2886] | 267 | if ( !create ) |
---|
| 268 | return NULL; |
---|
| 269 | |
---|
| 270 | if ( !need_new ) { |
---|
| 271 | for (i = 0; i < config->codecs.num; i++) { |
---|
| 272 | if ( config->codecs.codec[i].codec == -1 ) { |
---|
| 273 | memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec)); |
---|
| 274 | config->codecs.codec[i].codec = codec; |
---|
| 275 | return &(config->codecs.codec[i]); |
---|
| 276 | } |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | if ( config->codecs.num == 0 ) { |
---|
| 281 | config->codecs.codec = malloc(16*sizeof(struct roar_libroar_config_codec)); |
---|
| 282 | } else { |
---|
| 283 | config->codecs.codec = realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec)); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | if ( config->codecs.codec == NULL ) |
---|
| 287 | return NULL; |
---|
| 288 | |
---|
| 289 | memset(&(config->codecs.codec[config->codecs.num]), 0, 16); |
---|
| 290 | for (i = config->codecs.num; i < (config->codecs.num+16); i++) { |
---|
| 291 | config->codecs.codec[i].codec = -1; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | i = config->codecs.num; |
---|
| 295 | config->codecs.num += 16; |
---|
| 296 | |
---|
[2913] | 297 | memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec)); |
---|
| 298 | config->codecs.codec[i].codec = codec; |
---|
| 299 | |
---|
[2886] | 300 | return &(config->codecs.codec[i]); |
---|
| 301 | } |
---|
| 302 | |
---|
[2567] | 303 | int roar_libroar_set_server(char * server) { |
---|
| 304 | roar_libroar_get_config_ptr()->server = server; |
---|
| 305 | return 0; |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | char * roar_libroar_get_server(void) { |
---|
| 309 | return roar_libroar_get_config_ptr()->server; |
---|
| 310 | } |
---|
[2478] | 311 | |
---|
[2477] | 312 | //ll |
---|