[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 | |
---|
[2478] | 37 | struct roar_libroar_config * roar_libroar_get_config_ptr(void) { |
---|
| 38 | static struct roar_libroar_config config; |
---|
| 39 | static int inited = 0; |
---|
| 40 | |
---|
| 41 | if ( !inited ) { |
---|
| 42 | memset(&config, 0, sizeof(config)); |
---|
[2567] | 43 | |
---|
| 44 | config.server = NULL; |
---|
| 45 | |
---|
[2478] | 46 | inited++; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | return &config; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | struct roar_libroar_config * roar_libroar_get_config(void) { |
---|
| 53 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
| 54 | static int inited = 0; |
---|
[2884] | 55 | char * next; |
---|
[2478] | 56 | |
---|
| 57 | if ( !inited ) { |
---|
[2480] | 58 | next = getenv("ROAR_OPTIONS"); |
---|
| 59 | |
---|
[2884] | 60 | if ( next != NULL ) { |
---|
| 61 | roar_libroar_config_parse(next, " "); |
---|
[2480] | 62 | } |
---|
| 63 | |
---|
[2478] | 64 | inited++; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | return config; |
---|
| 68 | } |
---|
| 69 | |
---|
[2890] | 70 | #define _P_FP(v) ((int)(atof((v))*256.0)) |
---|
| 71 | #define _P_INT(v) (atoi((v))) |
---|
| 72 | #define _P_BOOL(v) (*(v) == 'y' || *(v) == 'j' || *(v) == 't' || *(v) == '1' ? 1 : 0) |
---|
| 73 | |
---|
| 74 | static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) { |
---|
| 75 | struct roar_libroar_config_codec * codec_cfg; |
---|
| 76 | int codec; |
---|
| 77 | char * codec_str, * option_str, * value_str; |
---|
| 78 | |
---|
| 79 | if ( config == NULL || txt == NULL ) |
---|
| 80 | return -1; |
---|
| 81 | |
---|
[2892] | 82 | codec_str = strtok(txt, ":"); |
---|
[2890] | 83 | |
---|
[2892] | 84 | if ( codec_str == NULL ) |
---|
| 85 | return -1; |
---|
| 86 | |
---|
| 87 | option_str = strtok(NULL, ":"); |
---|
[2890] | 88 | |
---|
| 89 | if ( option_str == NULL ) |
---|
| 90 | return -1; |
---|
| 91 | |
---|
[2892] | 92 | value_str = strtok(NULL, ":"); |
---|
[2890] | 93 | |
---|
| 94 | if ( value_str == NULL ) |
---|
| 95 | return -1; |
---|
| 96 | |
---|
| 97 | if ( (codec = roar_str2codec(codec_str)) == -1 ) { |
---|
| 98 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec: %s", codec_str); |
---|
| 99 | return -1; |
---|
| 100 | } |
---|
| 101 | |
---|
[2893] | 102 | if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL ) |
---|
[2890] | 103 | return -1; |
---|
| 104 | |
---|
| 105 | if ( !strcmp(option_str, "q") || !strcmp(option_str, "quality") ) { |
---|
| 106 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_Q; |
---|
| 107 | codec_cfg->q = _P_FP(value_str); |
---|
| 108 | } else if ( !strcmp(option_str, "complexity") ) { |
---|
| 109 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY; |
---|
| 110 | codec_cfg->complexity = _P_FP(value_str); |
---|
| 111 | } else if ( !strcmp(option_str, "dtx") ) { |
---|
| 112 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_DTX; |
---|
| 113 | codec_cfg->dtx = _P_BOOL(value_str); |
---|
| 114 | } else if ( !strcmp(option_str, "cc-max") ) { |
---|
| 115 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MAX_CC; |
---|
| 116 | codec_cfg->max_cc = _P_INT(value_str); |
---|
| 117 | } else { |
---|
[2891] | 118 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str); |
---|
[2892] | 119 | return -1; |
---|
[2890] | 120 | } |
---|
| 121 | |
---|
[2892] | 122 | return 0; |
---|
[2890] | 123 | } |
---|
| 124 | |
---|
[2884] | 125 | int roar_libroar_config_parse(char * txt, char * delm) { |
---|
| 126 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
| 127 | char * k, * v, * next = txt; |
---|
| 128 | |
---|
| 129 | while (next != NULL) { |
---|
| 130 | k = next; |
---|
[2886] | 131 | |
---|
| 132 | if ( delm == NULL ) { |
---|
| 133 | // no delm -> we have only one option |
---|
| 134 | next = NULL; |
---|
| 135 | } else { |
---|
| 136 | next = strstr(next, delm); |
---|
| 137 | } |
---|
| 138 | |
---|
[2884] | 139 | if ( next != NULL ) { |
---|
| 140 | *next = 0; |
---|
| 141 | next++; |
---|
| 142 | } |
---|
| 143 | |
---|
[2888] | 144 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 145 | |
---|
[2887] | 146 | // strip leading spaces: |
---|
| 147 | while ( *k == ' ' ) k++; |
---|
| 148 | |
---|
[2888] | 149 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 150 | |
---|
[2887] | 151 | // strip tailing new lions: |
---|
| 152 | v = strtok(k, "\r\n"); |
---|
| 153 | if ( v != NULL ) { |
---|
| 154 | if ( *v == '\r' || *v == '\n' ) |
---|
| 155 | *v = 0; |
---|
| 156 | } |
---|
| 157 | |
---|
[2888] | 158 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 159 | |
---|
[2887] | 160 | // comments |
---|
| 161 | if ( *k == '#' ) |
---|
| 162 | continue; |
---|
| 163 | |
---|
[2888] | 164 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 165 | |
---|
[2887] | 166 | // empty options: |
---|
| 167 | if ( *k == 0 ) |
---|
| 168 | continue; |
---|
| 169 | |
---|
[2884] | 170 | if ( (v = strstr(k, ":")) != NULL ) { |
---|
| 171 | *v = 0; |
---|
| 172 | v++; |
---|
| 173 | } |
---|
| 174 | |
---|
[2888] | 175 | ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v); |
---|
| 176 | |
---|
[2884] | 177 | if ( !strcmp(k, "workaround") ) { |
---|
| 178 | if ( !strcmp(v, "use-execed") ) { |
---|
| 179 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED; |
---|
| 180 | } else { |
---|
[2888] | 181 | ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v); |
---|
[2884] | 182 | } |
---|
| 183 | } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) { |
---|
| 184 | if ( !strcmp(v, "sysio") ) { |
---|
| 185 | config->warnings.sysio = ROAR_WARNING_ALWAYS; |
---|
| 186 | } else { |
---|
[2888] | 187 | ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v); |
---|
[2884] | 188 | } |
---|
[2890] | 189 | } else if ( !strcmp(k, "codec") ) { |
---|
| 190 | if ( roar_libroar_config_parse_codec(config, v) == -1 ) { |
---|
| 191 | ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option"); |
---|
| 192 | } |
---|
[2884] | 193 | } else { |
---|
[2888] | 194 | ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k); |
---|
[2884] | 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | return 0; |
---|
| 199 | } |
---|
| 200 | |
---|
[2886] | 201 | struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) { |
---|
[2893] | 202 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
| 203 | return roar_libroar_config_codec_get_conf(codec, create, config); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | static struct roar_libroar_config_codec * |
---|
| 207 | roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config) { |
---|
[2886] | 208 | int i; |
---|
| 209 | int need_new = 1; |
---|
| 210 | |
---|
| 211 | if ( codec < 0 || create < 0 ) |
---|
| 212 | return NULL; |
---|
| 213 | |
---|
| 214 | if ( config->codecs.num == 0 ) { |
---|
| 215 | // no match case: |
---|
| 216 | if ( !create ) |
---|
| 217 | return NULL; |
---|
| 218 | } else { |
---|
| 219 | for (i = 0; i < config->codecs.num; i++) { |
---|
| 220 | if ( config->codecs.codec[i].codec == codec ) |
---|
| 221 | return &(config->codecs.codec[i]); |
---|
| 222 | if ( config->codecs.codec[i].codec == -1 ) |
---|
| 223 | need_new = 0; |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | if ( !create ) |
---|
| 228 | return NULL; |
---|
| 229 | |
---|
| 230 | if ( !need_new ) { |
---|
| 231 | for (i = 0; i < config->codecs.num; i++) { |
---|
| 232 | if ( config->codecs.codec[i].codec == -1 ) { |
---|
| 233 | memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec)); |
---|
| 234 | config->codecs.codec[i].codec = codec; |
---|
| 235 | return &(config->codecs.codec[i]); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | if ( config->codecs.num == 0 ) { |
---|
| 241 | config->codecs.codec = malloc(16*sizeof(struct roar_libroar_config_codec)); |
---|
| 242 | } else { |
---|
| 243 | config->codecs.codec = realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec)); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | if ( config->codecs.codec == NULL ) |
---|
| 247 | return NULL; |
---|
| 248 | |
---|
| 249 | memset(&(config->codecs.codec[config->codecs.num]), 0, 16); |
---|
| 250 | for (i = config->codecs.num; i < (config->codecs.num+16); i++) { |
---|
| 251 | config->codecs.codec[i].codec = -1; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | i = config->codecs.num; |
---|
| 255 | config->codecs.num += 16; |
---|
| 256 | |
---|
| 257 | return &(config->codecs.codec[i]); |
---|
| 258 | } |
---|
| 259 | |
---|
[2567] | 260 | int roar_libroar_set_server(char * server) { |
---|
| 261 | roar_libroar_get_config_ptr()->server = server; |
---|
| 262 | return 0; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | char * roar_libroar_get_server(void) { |
---|
| 266 | return roar_libroar_get_config_ptr()->server; |
---|
| 267 | } |
---|
[2478] | 268 | |
---|
[2477] | 269 | //ll |
---|