[2477] | 1 | //config.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[3811] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010 |
---|
[2477] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[2477] | 23 | * |
---|
| 24 | * NOTE for everyone want's to change something and send patches: |
---|
| 25 | * read README and HACKING! There a addition information on |
---|
| 26 | * the license of this document you need to read before you send |
---|
| 27 | * any patches. |
---|
| 28 | * |
---|
| 29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 30 | * or libpulse*: |
---|
| 31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | #include "libroar.h" |
---|
| 37 | |
---|
[2895] | 38 | static struct roar_libroar_config_codec * |
---|
| 39 | roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config); |
---|
| 40 | |
---|
| 41 | |
---|
[2478] | 42 | struct roar_libroar_config * roar_libroar_get_config_ptr(void) { |
---|
| 43 | static struct roar_libroar_config config; |
---|
[3218] | 44 | static int inited = 0; |
---|
| 45 | static char authfile[1024]; |
---|
[4349] | 46 | char * home = roar_env_get_home(0); |
---|
[2478] | 47 | |
---|
| 48 | if ( !inited ) { |
---|
| 49 | memset(&config, 0, sizeof(config)); |
---|
[2567] | 50 | |
---|
[3218] | 51 | config.server = NULL; |
---|
| 52 | config.authfile = NULL; |
---|
| 53 | |
---|
| 54 | if ( home != NULL ) { |
---|
| 55 | snprintf(authfile, 1023, "%s/.roarauth", home); |
---|
[3375] | 56 | authfile[1023] = 0; |
---|
| 57 | config.authfile = authfile; |
---|
[4348] | 58 | config.serversfile = NULL; |
---|
[3218] | 59 | } |
---|
[2567] | 60 | |
---|
[2478] | 61 | inited++; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | return &config; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | struct roar_libroar_config * roar_libroar_get_config(void) { |
---|
| 68 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
| 69 | static int inited = 0; |
---|
[2884] | 70 | char * next; |
---|
[2478] | 71 | |
---|
| 72 | if ( !inited ) { |
---|
[2480] | 73 | next = getenv("ROAR_OPTIONS"); |
---|
| 74 | |
---|
[2884] | 75 | if ( next != NULL ) { |
---|
| 76 | roar_libroar_config_parse(next, " "); |
---|
[2480] | 77 | } |
---|
| 78 | |
---|
[2478] | 79 | inited++; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | return config; |
---|
| 83 | } |
---|
| 84 | |
---|
[2890] | 85 | #define _P_FP(v) ((int)(atof((v))*256.0)) |
---|
| 86 | #define _P_INT(v) (atoi((v))) |
---|
| 87 | #define _P_BOOL(v) (*(v) == 'y' || *(v) == 'j' || *(v) == 't' || *(v) == '1' ? 1 : 0) |
---|
| 88 | |
---|
| 89 | static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) { |
---|
| 90 | struct roar_libroar_config_codec * codec_cfg; |
---|
| 91 | int codec; |
---|
| 92 | char * codec_str, * option_str, * value_str; |
---|
| 93 | |
---|
[2912] | 94 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
| 95 | |
---|
[2890] | 96 | if ( config == NULL || txt == NULL ) |
---|
| 97 | return -1; |
---|
| 98 | |
---|
[2912] | 99 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
| 100 | |
---|
[2892] | 101 | codec_str = strtok(txt, ":"); |
---|
[2890] | 102 | |
---|
[2892] | 103 | if ( codec_str == NULL ) |
---|
| 104 | return -1; |
---|
| 105 | |
---|
| 106 | option_str = strtok(NULL, ":"); |
---|
[2890] | 107 | |
---|
| 108 | if ( option_str == NULL ) |
---|
| 109 | return -1; |
---|
| 110 | |
---|
[2892] | 111 | value_str = strtok(NULL, ":"); |
---|
[2890] | 112 | |
---|
| 113 | if ( value_str == NULL ) |
---|
| 114 | return -1; |
---|
| 115 | |
---|
[2912] | 116 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
| 117 | |
---|
[2890] | 118 | if ( (codec = roar_str2codec(codec_str)) == -1 ) { |
---|
| 119 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec: %s", codec_str); |
---|
| 120 | return -1; |
---|
| 121 | } |
---|
| 122 | |
---|
[2912] | 123 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec); |
---|
| 124 | |
---|
[2893] | 125 | if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL ) |
---|
[2890] | 126 | return -1; |
---|
| 127 | |
---|
[2912] | 128 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg); |
---|
| 129 | |
---|
[2890] | 130 | if ( !strcmp(option_str, "q") || !strcmp(option_str, "quality") ) { |
---|
| 131 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_Q; |
---|
| 132 | codec_cfg->q = _P_FP(value_str); |
---|
| 133 | } else if ( !strcmp(option_str, "complexity") ) { |
---|
| 134 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY; |
---|
| 135 | codec_cfg->complexity = _P_FP(value_str); |
---|
| 136 | } else if ( !strcmp(option_str, "dtx") ) { |
---|
| 137 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_DTX; |
---|
| 138 | codec_cfg->dtx = _P_BOOL(value_str); |
---|
| 139 | } else if ( !strcmp(option_str, "cc-max") ) { |
---|
| 140 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MAX_CC; |
---|
| 141 | codec_cfg->max_cc = _P_INT(value_str); |
---|
[2926] | 142 | } else if ( !strcmp(option_str, "vbr") ) { |
---|
| 143 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_VBR; |
---|
| 144 | codec_cfg->vbr = _P_BOOL(value_str); |
---|
[2962] | 145 | } else if ( !strcmp(option_str, "mode") ) { |
---|
| 146 | if ( !strcmp(value_str, "nb") ) { |
---|
| 147 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE; |
---|
| 148 | codec_cfg->mode = ROAR_LIBROAR_CONFIG_MODE_NB; |
---|
| 149 | } else if ( !strcmp(value_str, "wb") ) { |
---|
| 150 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE; |
---|
| 151 | codec_cfg->mode = ROAR_LIBROAR_CONFIG_MODE_WB; |
---|
| 152 | } else if ( !strcmp(value_str, "uwb") ) { |
---|
| 153 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE; |
---|
| 154 | codec_cfg->mode = ROAR_LIBROAR_CONFIG_MODE_UWB; |
---|
| 155 | } else { |
---|
| 156 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec mode: %s", value_str); |
---|
| 157 | return -1; |
---|
| 158 | } |
---|
[2890] | 159 | } else { |
---|
[2891] | 160 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str); |
---|
[2892] | 161 | return -1; |
---|
[2890] | 162 | } |
---|
| 163 | |
---|
[2892] | 164 | return 0; |
---|
[2890] | 165 | } |
---|
| 166 | |
---|
[2884] | 167 | int roar_libroar_config_parse(char * txt, char * delm) { |
---|
| 168 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
| 169 | char * k, * v, * next = txt; |
---|
| 170 | |
---|
| 171 | while (next != NULL) { |
---|
| 172 | k = next; |
---|
[2886] | 173 | |
---|
| 174 | if ( delm == NULL ) { |
---|
| 175 | // no delm -> we have only one option |
---|
| 176 | next = NULL; |
---|
| 177 | } else { |
---|
| 178 | next = strstr(next, delm); |
---|
| 179 | } |
---|
| 180 | |
---|
[2884] | 181 | if ( next != NULL ) { |
---|
| 182 | *next = 0; |
---|
| 183 | next++; |
---|
| 184 | } |
---|
| 185 | |
---|
[2888] | 186 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 187 | |
---|
[2887] | 188 | // strip leading spaces: |
---|
| 189 | while ( *k == ' ' ) k++; |
---|
| 190 | |
---|
[2888] | 191 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 192 | |
---|
[2887] | 193 | // strip tailing new lions: |
---|
| 194 | v = strtok(k, "\r\n"); |
---|
| 195 | if ( v != NULL ) { |
---|
| 196 | if ( *v == '\r' || *v == '\n' ) |
---|
| 197 | *v = 0; |
---|
| 198 | } |
---|
| 199 | |
---|
[2888] | 200 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 201 | |
---|
[2887] | 202 | // comments |
---|
| 203 | if ( *k == '#' ) |
---|
| 204 | continue; |
---|
| 205 | |
---|
[2888] | 206 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 207 | |
---|
[2887] | 208 | // empty options: |
---|
| 209 | if ( *k == 0 ) |
---|
| 210 | continue; |
---|
| 211 | |
---|
[2884] | 212 | if ( (v = strstr(k, ":")) != NULL ) { |
---|
| 213 | *v = 0; |
---|
| 214 | v++; |
---|
| 215 | } |
---|
| 216 | |
---|
[2888] | 217 | ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v); |
---|
| 218 | |
---|
[2884] | 219 | if ( !strcmp(k, "workaround") ) { |
---|
| 220 | if ( !strcmp(v, "use-execed") ) { |
---|
| 221 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED; |
---|
| 222 | } else { |
---|
[2888] | 223 | ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v); |
---|
[2884] | 224 | } |
---|
| 225 | } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) { |
---|
| 226 | if ( !strcmp(v, "sysio") ) { |
---|
| 227 | config->warnings.sysio = ROAR_WARNING_ALWAYS; |
---|
[3855] | 228 | } else if ( !strcmp(v, "obsolete") ) { |
---|
| 229 | config->warnings.obsolete = ROAR_WARNING_ALWAYS; |
---|
| 230 | } else if ( !strcmp(v, "all") ) { |
---|
| 231 | config->warnings.sysio = ROAR_WARNING_ALWAYS; |
---|
| 232 | config->warnings.obsolete = ROAR_WARNING_ALWAYS; |
---|
[2884] | 233 | } else { |
---|
[2888] | 234 | ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v); |
---|
[2884] | 235 | } |
---|
[3136] | 236 | } else if ( !strcmp(k, "force-rate") ) { |
---|
| 237 | config->info.rate = atoi(v); |
---|
| 238 | } else if ( !strcmp(k, "force-bits") ) { |
---|
| 239 | config->info.bits = atoi(v); |
---|
| 240 | } else if ( !strcmp(k, "force-channels") ) { |
---|
| 241 | config->info.channels = atoi(v); |
---|
| 242 | } else if ( !strcmp(k, "force-codec") ) { |
---|
| 243 | config->info.codec = roar_str2codec(v); |
---|
[2890] | 244 | } else if ( !strcmp(k, "codec") ) { |
---|
| 245 | if ( roar_libroar_config_parse_codec(config, v) == -1 ) { |
---|
| 246 | ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option"); |
---|
| 247 | } |
---|
[2896] | 248 | } else if ( !strcmp(k, "set-server") ) { |
---|
[2899] | 249 | if ( roar_libroar_get_server() == NULL ) |
---|
| 250 | roar_libroar_set_server(v); |
---|
[3218] | 251 | } else if ( !strcmp(k, "set-authfile") ) { |
---|
[3219] | 252 | strncpy(config->authfile, v, 1023); |
---|
| 253 | config->authfile[1023] = 0; |
---|
[3375] | 254 | } else if ( !strcmp(k, "x11-display") ) { |
---|
| 255 | config->x11.display = v; |
---|
[2884] | 256 | } else { |
---|
[2888] | 257 | ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k); |
---|
[2884] | 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | return 0; |
---|
| 262 | } |
---|
| 263 | |
---|
[2886] | 264 | struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) { |
---|
[2893] | 265 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
| 266 | return roar_libroar_config_codec_get_conf(codec, create, config); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | static struct roar_libroar_config_codec * |
---|
| 270 | roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config) { |
---|
[2886] | 271 | int i; |
---|
| 272 | int need_new = 1; |
---|
| 273 | |
---|
[2912] | 274 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
| 275 | |
---|
[2886] | 276 | if ( codec < 0 || create < 0 ) |
---|
| 277 | return NULL; |
---|
| 278 | |
---|
[2912] | 279 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
| 280 | |
---|
[2886] | 281 | if ( config->codecs.num == 0 ) { |
---|
| 282 | // no match case: |
---|
| 283 | if ( !create ) |
---|
| 284 | return NULL; |
---|
| 285 | } else { |
---|
| 286 | for (i = 0; i < config->codecs.num; i++) { |
---|
| 287 | if ( config->codecs.codec[i].codec == codec ) |
---|
| 288 | return &(config->codecs.codec[i]); |
---|
| 289 | if ( config->codecs.codec[i].codec == -1 ) |
---|
| 290 | need_new = 0; |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | |
---|
[2912] | 294 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
| 295 | |
---|
[2886] | 296 | if ( !create ) |
---|
| 297 | return NULL; |
---|
| 298 | |
---|
| 299 | if ( !need_new ) { |
---|
| 300 | for (i = 0; i < config->codecs.num; i++) { |
---|
| 301 | if ( config->codecs.codec[i].codec == -1 ) { |
---|
| 302 | memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec)); |
---|
| 303 | config->codecs.codec[i].codec = codec; |
---|
| 304 | return &(config->codecs.codec[i]); |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | if ( config->codecs.num == 0 ) { |
---|
| 310 | config->codecs.codec = malloc(16*sizeof(struct roar_libroar_config_codec)); |
---|
| 311 | } else { |
---|
| 312 | config->codecs.codec = realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec)); |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | if ( config->codecs.codec == NULL ) |
---|
| 316 | return NULL; |
---|
| 317 | |
---|
| 318 | memset(&(config->codecs.codec[config->codecs.num]), 0, 16); |
---|
| 319 | for (i = config->codecs.num; i < (config->codecs.num+16); i++) { |
---|
| 320 | config->codecs.codec[i].codec = -1; |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | i = config->codecs.num; |
---|
| 324 | config->codecs.num += 16; |
---|
| 325 | |
---|
[2913] | 326 | memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec)); |
---|
| 327 | config->codecs.codec[i].codec = codec; |
---|
| 328 | |
---|
[2886] | 329 | return &(config->codecs.codec[i]); |
---|
| 330 | } |
---|
| 331 | |
---|
[2567] | 332 | int roar_libroar_set_server(char * server) { |
---|
| 333 | roar_libroar_get_config_ptr()->server = server; |
---|
| 334 | return 0; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | char * roar_libroar_get_server(void) { |
---|
| 338 | return roar_libroar_get_config_ptr()->server; |
---|
| 339 | } |
---|
[2478] | 340 | |
---|
[3855] | 341 | void roar_libroar_nowarn(void) { |
---|
| 342 | roar_libroar_get_config_ptr()->nowarncounter++; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | void roar_libroar_warn(void) { |
---|
| 346 | struct roar_libroar_config * cfg = roar_libroar_get_config_ptr(); |
---|
| 347 | |
---|
| 348 | if ( cfg->nowarncounter == 0 ) { |
---|
| 349 | ROAR_WARN("roar_libroar_warn(): Re-Enabling already enabled warnings! (Application error?)"); |
---|
| 350 | return; |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | cfg->nowarncounter--; |
---|
| 354 | } |
---|
| 355 | |
---|
[2477] | 356 | //ll |
---|