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