source: roaraudio/libroar/config.c @ 5407:edd703e264d0

Last change on this file since 5407:edd703e264d0 was 5381:430b1d26e12d, checked in by phi, 12 years ago

updated copyright years

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