source: roaraudio/libroar/config.c @ 5695:dd52b51afdfb

Last change on this file since 5695:dd52b51afdfb was 5466:201ac6bd51ee, checked in by phi, 12 years ago

accept symbolic names in force-* options

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