[2477] | 1 | //config.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011 |
---|
[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]; |
---|
[4517] | 46 | const char * home = roar_env_get_home(0); |
---|
[2478] | 47 | |
---|
| 48 | if ( !inited ) { |
---|
| 49 | memset(&config, 0, sizeof(config)); |
---|
[2567] | 50 | |
---|
[4779] | 51 | #ifdef ROAR_SUPPORT_TRAP |
---|
| 52 | config.trap_policy = ROAR_TRAP_IGNORE; |
---|
| 53 | #endif |
---|
[3218] | 54 | config.server = NULL; |
---|
| 55 | config.authfile = NULL; |
---|
| 56 | |
---|
| 57 | if ( home != NULL ) { |
---|
| 58 | snprintf(authfile, 1023, "%s/.roarauth", home); |
---|
[3375] | 59 | authfile[1023] = 0; |
---|
| 60 | config.authfile = authfile; |
---|
[4348] | 61 | config.serversfile = NULL; |
---|
[3218] | 62 | } |
---|
[2567] | 63 | |
---|
[2478] | 64 | inited++; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | return &config; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | struct roar_libroar_config * roar_libroar_get_config(void) { |
---|
| 71 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
| 72 | static int inited = 0; |
---|
[2884] | 73 | char * next; |
---|
[2478] | 74 | |
---|
| 75 | if ( !inited ) { |
---|
[2480] | 76 | next = getenv("ROAR_OPTIONS"); |
---|
| 77 | |
---|
[2884] | 78 | if ( next != NULL ) { |
---|
| 79 | roar_libroar_config_parse(next, " "); |
---|
[2480] | 80 | } |
---|
| 81 | |
---|
[2478] | 82 | inited++; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | return config; |
---|
| 86 | } |
---|
| 87 | |
---|
[5010] | 88 | int roar_libroar_reset_config(void) { |
---|
| 89 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
| 90 | |
---|
| 91 | if ( config->codecs.codec != NULL ) { |
---|
| 92 | roar_mm_free(config->codecs.codec); |
---|
| 93 | config->codecs.num = 0; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | return 0; |
---|
| 97 | } |
---|
| 98 | |
---|
[2890] | 99 | #define _P_FP(v) ((int)(atof((v))*256.0)) |
---|
| 100 | #define _P_INT(v) (atoi((v))) |
---|
| 101 | #define _P_BOOL(v) (*(v) == 'y' || *(v) == 'j' || *(v) == 't' || *(v) == '1' ? 1 : 0) |
---|
| 102 | |
---|
| 103 | static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) { |
---|
| 104 | struct roar_libroar_config_codec * codec_cfg; |
---|
| 105 | int codec; |
---|
| 106 | char * codec_str, * option_str, * value_str; |
---|
[5006] | 107 | char * toksave = NULL; |
---|
[2890] | 108 | |
---|
[2912] | 109 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
| 110 | |
---|
[2890] | 111 | if ( config == NULL || txt == NULL ) |
---|
| 112 | return -1; |
---|
| 113 | |
---|
[2912] | 114 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
| 115 | |
---|
[5006] | 116 | codec_str = roar_mm_strtok_r(txt, ":", &toksave); |
---|
[2890] | 117 | |
---|
[2892] | 118 | if ( codec_str == NULL ) |
---|
| 119 | return -1; |
---|
| 120 | |
---|
[5006] | 121 | option_str = roar_mm_strtok_r(NULL, ":", &toksave); |
---|
[2890] | 122 | |
---|
| 123 | if ( option_str == NULL ) |
---|
| 124 | return -1; |
---|
| 125 | |
---|
[5006] | 126 | value_str = roar_mm_strtok_r(NULL, ":", &toksave); |
---|
[2890] | 127 | |
---|
| 128 | if ( value_str == NULL ) |
---|
| 129 | return -1; |
---|
| 130 | |
---|
[2912] | 131 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
| 132 | |
---|
[2890] | 133 | if ( (codec = roar_str2codec(codec_str)) == -1 ) { |
---|
| 134 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec: %s", codec_str); |
---|
| 135 | return -1; |
---|
| 136 | } |
---|
| 137 | |
---|
[2912] | 138 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec); |
---|
| 139 | |
---|
[2893] | 140 | if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL ) |
---|
[2890] | 141 | return -1; |
---|
| 142 | |
---|
[2912] | 143 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg); |
---|
| 144 | |
---|
[2890] | 145 | if ( !strcmp(option_str, "q") || !strcmp(option_str, "quality") ) { |
---|
| 146 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_Q; |
---|
| 147 | codec_cfg->q = _P_FP(value_str); |
---|
| 148 | } else if ( !strcmp(option_str, "complexity") ) { |
---|
| 149 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY; |
---|
| 150 | codec_cfg->complexity = _P_FP(value_str); |
---|
| 151 | } else if ( !strcmp(option_str, "dtx") ) { |
---|
| 152 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_DTX; |
---|
| 153 | codec_cfg->dtx = _P_BOOL(value_str); |
---|
| 154 | } else if ( !strcmp(option_str, "cc-max") ) { |
---|
| 155 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MAX_CC; |
---|
| 156 | codec_cfg->max_cc = _P_INT(value_str); |
---|
[2926] | 157 | } else if ( !strcmp(option_str, "vbr") ) { |
---|
| 158 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_VBR; |
---|
| 159 | codec_cfg->vbr = _P_BOOL(value_str); |
---|
[2962] | 160 | } else if ( !strcmp(option_str, "mode") ) { |
---|
| 161 | if ( !strcmp(value_str, "nb") ) { |
---|
| 162 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE; |
---|
| 163 | codec_cfg->mode = ROAR_LIBROAR_CONFIG_MODE_NB; |
---|
| 164 | } else if ( !strcmp(value_str, "wb") ) { |
---|
| 165 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE; |
---|
| 166 | codec_cfg->mode = ROAR_LIBROAR_CONFIG_MODE_WB; |
---|
| 167 | } else if ( !strcmp(value_str, "uwb") ) { |
---|
| 168 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE; |
---|
| 169 | codec_cfg->mode = ROAR_LIBROAR_CONFIG_MODE_UWB; |
---|
| 170 | } else { |
---|
| 171 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec mode: %s", value_str); |
---|
| 172 | return -1; |
---|
| 173 | } |
---|
[2890] | 174 | } else { |
---|
[2891] | 175 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str); |
---|
[2892] | 176 | return -1; |
---|
[2890] | 177 | } |
---|
| 178 | |
---|
[2892] | 179 | return 0; |
---|
[2890] | 180 | } |
---|
| 181 | |
---|
[2884] | 182 | int roar_libroar_config_parse(char * txt, char * delm) { |
---|
| 183 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
[5006] | 184 | ssize_t len; |
---|
[2884] | 185 | char * k, * v, * next = txt; |
---|
| 186 | |
---|
| 187 | while (next != NULL) { |
---|
| 188 | k = next; |
---|
[2886] | 189 | |
---|
| 190 | if ( delm == NULL ) { |
---|
| 191 | // no delm -> we have only one option |
---|
| 192 | next = NULL; |
---|
| 193 | } else { |
---|
| 194 | next = strstr(next, delm); |
---|
| 195 | } |
---|
| 196 | |
---|
[2884] | 197 | if ( next != NULL ) { |
---|
| 198 | *next = 0; |
---|
| 199 | next++; |
---|
| 200 | } |
---|
| 201 | |
---|
[2888] | 202 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 203 | |
---|
[2887] | 204 | // strip leading spaces: |
---|
| 205 | while ( *k == ' ' ) k++; |
---|
| 206 | |
---|
[2888] | 207 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 208 | |
---|
[2887] | 209 | // strip tailing new lions: |
---|
[5006] | 210 | len = roar_mm_strlen(k); |
---|
| 211 | if ( len != -1 ) |
---|
| 212 | for (len--; len && (k[len] == '\r' || k[len] == '\n'); len--) |
---|
| 213 | k[len] = 0; |
---|
[2887] | 214 | |
---|
[2888] | 215 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 216 | |
---|
[2887] | 217 | // comments |
---|
| 218 | if ( *k == '#' ) |
---|
| 219 | continue; |
---|
| 220 | |
---|
[2888] | 221 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 222 | |
---|
[2887] | 223 | // empty options: |
---|
| 224 | if ( *k == 0 ) |
---|
| 225 | continue; |
---|
| 226 | |
---|
[2884] | 227 | if ( (v = strstr(k, ":")) != NULL ) { |
---|
| 228 | *v = 0; |
---|
| 229 | v++; |
---|
| 230 | } |
---|
| 231 | |
---|
[2888] | 232 | ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v); |
---|
| 233 | |
---|
[2884] | 234 | if ( !strcmp(k, "workaround") ) { |
---|
| 235 | if ( !strcmp(v, "use-execed") ) { |
---|
| 236 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED; |
---|
[4653] | 237 | } else if ( !strcmp(v, "no-slp") ) { |
---|
| 238 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP; |
---|
[2884] | 239 | } else { |
---|
[2888] | 240 | ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v); |
---|
[2884] | 241 | } |
---|
| 242 | } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) { |
---|
| 243 | if ( !strcmp(v, "sysio") ) { |
---|
| 244 | config->warnings.sysio = ROAR_WARNING_ALWAYS; |
---|
[3855] | 245 | } else if ( !strcmp(v, "obsolete") ) { |
---|
| 246 | config->warnings.obsolete = ROAR_WARNING_ALWAYS; |
---|
| 247 | } else if ( !strcmp(v, "all") ) { |
---|
| 248 | config->warnings.sysio = ROAR_WARNING_ALWAYS; |
---|
| 249 | config->warnings.obsolete = ROAR_WARNING_ALWAYS; |
---|
[2884] | 250 | } else { |
---|
[2888] | 251 | ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v); |
---|
[2884] | 252 | } |
---|
[4779] | 253 | #ifdef ROAR_SUPPORT_TRAP |
---|
| 254 | } else if ( !strcmp(k, "trap-policy") ) { |
---|
| 255 | if ( !strcmp(v, "ignore") ) { |
---|
| 256 | config->trap_policy = ROAR_TRAP_IGNORE; |
---|
| 257 | } else if ( !strcmp(v, "warn") ) { |
---|
| 258 | config->trap_policy = ROAR_TRAP_WARN; |
---|
| 259 | } else if ( !strcmp(v, "abort") ) { |
---|
| 260 | config->trap_policy = ROAR_TRAP_ABORT; |
---|
| 261 | #ifdef SIGKILL |
---|
| 262 | } else if ( !strcmp(v, "kill") ) { |
---|
| 263 | config->trap_policy = ROAR_TRAP_KILL; |
---|
| 264 | #endif |
---|
| 265 | #ifdef SIGSTOP |
---|
| 266 | } else if ( !strcmp(v, "stop") ) { |
---|
| 267 | config->trap_policy = ROAR_TRAP_STOP; |
---|
| 268 | #endif |
---|
[4784] | 269 | } else if ( !strcmp(v, "die") ) { |
---|
| 270 | config->trap_policy = ROAR_TRAP_DIE; |
---|
[4779] | 271 | } else { |
---|
| 272 | ROAR_WARN("roar_libroar_config_parse(*): Unknown trap policy: %s", v); |
---|
| 273 | } |
---|
| 274 | #endif |
---|
[3136] | 275 | } else if ( !strcmp(k, "force-rate") ) { |
---|
| 276 | config->info.rate = atoi(v); |
---|
| 277 | } else if ( !strcmp(k, "force-bits") ) { |
---|
| 278 | config->info.bits = atoi(v); |
---|
| 279 | } else if ( !strcmp(k, "force-channels") ) { |
---|
| 280 | config->info.channels = atoi(v); |
---|
| 281 | } else if ( !strcmp(k, "force-codec") ) { |
---|
| 282 | config->info.codec = roar_str2codec(v); |
---|
[2890] | 283 | } else if ( !strcmp(k, "codec") ) { |
---|
| 284 | if ( roar_libroar_config_parse_codec(config, v) == -1 ) { |
---|
| 285 | ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option"); |
---|
| 286 | } |
---|
[2896] | 287 | } else if ( !strcmp(k, "set-server") ) { |
---|
[2899] | 288 | if ( roar_libroar_get_server() == NULL ) |
---|
| 289 | roar_libroar_set_server(v); |
---|
[3218] | 290 | } else if ( !strcmp(k, "set-authfile") ) { |
---|
[3219] | 291 | strncpy(config->authfile, v, 1023); |
---|
| 292 | config->authfile[1023] = 0; |
---|
[3375] | 293 | } else if ( !strcmp(k, "x11-display") ) { |
---|
| 294 | config->x11.display = v; |
---|
[2884] | 295 | } else { |
---|
[2888] | 296 | ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k); |
---|
[2884] | 297 | } |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | return 0; |
---|
| 301 | } |
---|
| 302 | |
---|
[2886] | 303 | struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) { |
---|
[2893] | 304 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
| 305 | return roar_libroar_config_codec_get_conf(codec, create, config); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | static struct roar_libroar_config_codec * |
---|
| 309 | roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config) { |
---|
[2886] | 310 | int i; |
---|
| 311 | int need_new = 1; |
---|
| 312 | |
---|
[2912] | 313 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
| 314 | |
---|
[2886] | 315 | if ( codec < 0 || create < 0 ) |
---|
| 316 | return NULL; |
---|
| 317 | |
---|
[2912] | 318 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
| 319 | |
---|
[2886] | 320 | if ( config->codecs.num == 0 ) { |
---|
| 321 | // no match case: |
---|
| 322 | if ( !create ) |
---|
| 323 | return NULL; |
---|
| 324 | } else { |
---|
| 325 | for (i = 0; i < config->codecs.num; i++) { |
---|
| 326 | if ( config->codecs.codec[i].codec == codec ) |
---|
| 327 | return &(config->codecs.codec[i]); |
---|
| 328 | if ( config->codecs.codec[i].codec == -1 ) |
---|
| 329 | need_new = 0; |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
[2912] | 333 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
| 334 | |
---|
[2886] | 335 | if ( !create ) |
---|
| 336 | return NULL; |
---|
| 337 | |
---|
| 338 | if ( !need_new ) { |
---|
| 339 | for (i = 0; i < config->codecs.num; i++) { |
---|
| 340 | if ( config->codecs.codec[i].codec == -1 ) { |
---|
| 341 | memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec)); |
---|
| 342 | config->codecs.codec[i].codec = codec; |
---|
| 343 | return &(config->codecs.codec[i]); |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | if ( config->codecs.num == 0 ) { |
---|
[4957] | 349 | config->codecs.codec = roar_mm_malloc(16*sizeof(struct roar_libroar_config_codec)); |
---|
[2886] | 350 | } else { |
---|
[4957] | 351 | config->codecs.codec = roar_mm_realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec)); |
---|
[2886] | 352 | } |
---|
| 353 | |
---|
| 354 | if ( config->codecs.codec == NULL ) |
---|
| 355 | return NULL; |
---|
| 356 | |
---|
| 357 | memset(&(config->codecs.codec[config->codecs.num]), 0, 16); |
---|
| 358 | for (i = config->codecs.num; i < (config->codecs.num+16); i++) { |
---|
| 359 | config->codecs.codec[i].codec = -1; |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | i = config->codecs.num; |
---|
| 363 | config->codecs.num += 16; |
---|
| 364 | |
---|
[2913] | 365 | memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec)); |
---|
| 366 | config->codecs.codec[i].codec = codec; |
---|
| 367 | |
---|
[2886] | 368 | return &(config->codecs.codec[i]); |
---|
| 369 | } |
---|
| 370 | |
---|
[2567] | 371 | int roar_libroar_set_server(char * server) { |
---|
| 372 | roar_libroar_get_config_ptr()->server = server; |
---|
| 373 | return 0; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | char * roar_libroar_get_server(void) { |
---|
| 377 | return roar_libroar_get_config_ptr()->server; |
---|
| 378 | } |
---|
[2478] | 379 | |
---|
[3855] | 380 | void roar_libroar_nowarn(void) { |
---|
| 381 | roar_libroar_get_config_ptr()->nowarncounter++; |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | void roar_libroar_warn(void) { |
---|
| 385 | struct roar_libroar_config * cfg = roar_libroar_get_config_ptr(); |
---|
| 386 | |
---|
| 387 | if ( cfg->nowarncounter == 0 ) { |
---|
| 388 | ROAR_WARN("roar_libroar_warn(): Re-Enabling already enabled warnings! (Application error?)"); |
---|
| 389 | return; |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | cfg->nowarncounter--; |
---|
| 393 | } |
---|
| 394 | |
---|
[2477] | 395 | //ll |
---|