source: roaraudio/libroar/config.c @ 5010:065b9de0acf7

Last change on this file since 5010:065b9de0acf7 was 5010:065b9de0acf7, checked in by phi, 13 years ago

Updated roar_reset() (Closes: #131)

File size: 11.6 KB
Line 
1//config.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011
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
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
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
38static struct roar_libroar_config_codec *
39           roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config);
40
41
42struct roar_libroar_config * roar_libroar_get_config_ptr(void) {
43 static struct roar_libroar_config config;
44 static int    inited = 0;
45 static char   authfile[1024];
46 const  char * home = roar_env_get_home(0);
47
48 if ( !inited ) {
49  memset(&config, 0, sizeof(config));
50
51#ifdef ROAR_SUPPORT_TRAP
52  config.trap_policy = ROAR_TRAP_IGNORE;
53#endif
54  config.server   = NULL;
55  config.authfile = NULL;
56
57  if ( home != NULL ) {
58   snprintf(authfile, 1023, "%s/.roarauth", home);
59   authfile[1023]     = 0;
60   config.authfile    = authfile;
61   config.serversfile = NULL;
62  }
63
64  inited++;
65 }
66
67 return &config;
68}
69
70struct roar_libroar_config * roar_libroar_get_config(void) {
71 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
72 static int inited = 0;
73 char * next;
74
75 if ( !inited ) {
76  next = getenv("ROAR_OPTIONS");
77
78  if ( next != NULL ) {
79   roar_libroar_config_parse(next, " ");
80  }
81
82  inited++;
83 }
84
85 return config;
86}
87
88int    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
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
103static 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;
107 char * toksave = NULL;
108
109 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
110
111 if ( config == NULL || txt == NULL )
112  return -1;
113
114 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
115
116 codec_str = roar_mm_strtok_r(txt, ":", &toksave);
117
118 if ( codec_str == NULL )
119  return -1;
120
121 option_str = roar_mm_strtok_r(NULL, ":", &toksave);
122
123 if ( option_str == NULL )
124  return -1;
125
126 value_str = roar_mm_strtok_r(NULL, ":", &toksave);
127
128 if ( value_str == NULL )
129  return -1;
130
131 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
132
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
138 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec);
139
140 if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL )
141  return -1;
142
143 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg);
144
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);
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);
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  }
174 } else {
175  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str);
176  return -1;
177 }
178
179 return 0;
180}
181
182int    roar_libroar_config_parse(char * txt, char * delm) {
183 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
184 ssize_t len;
185 char * k, * v, * next = txt;
186
187 while (next != NULL) {
188  k = next;
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
197  if ( next != NULL ) {
198   *next = 0;
199    next++;
200  }
201
202  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
203
204  // strip leading spaces:
205  while ( *k == ' ' ) k++;
206
207  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
208
209  // strip tailing new lions:
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;
214
215  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
216
217  // comments
218  if ( *k == '#' )
219   continue;
220
221  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
222
223  // empty options:
224  if ( *k == 0 )
225   continue;
226
227  if ( (v = strstr(k, ":")) != NULL ) {
228   *v = 0;
229    v++;
230  }
231
232  ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v);
233
234  if ( !strcmp(k, "workaround") ) {
235   if ( !strcmp(v, "use-execed") ) {
236    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED;
237   } else if ( !strcmp(v, "no-slp") ) {
238    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP;
239   } else {
240    ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v);
241   }
242  } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) {
243   if ( !strcmp(v, "sysio") ) {
244    config->warnings.sysio = ROAR_WARNING_ALWAYS;
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;
250   } else {
251    ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v);
252   }
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
269   } else if ( !strcmp(v, "die") ) {
270    config->trap_policy = ROAR_TRAP_DIE;
271   } else {
272    ROAR_WARN("roar_libroar_config_parse(*): Unknown trap policy: %s", v);
273   }
274#endif
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);
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   }
287  } else if ( !strcmp(k, "set-server") ) {
288   if ( roar_libroar_get_server() == NULL )
289    roar_libroar_set_server(v);
290  } else if ( !strcmp(k, "set-authfile") ) {
291   strncpy(config->authfile, v, 1023);
292   config->authfile[1023] = 0;
293  } else if ( !strcmp(k, "x11-display") ) {
294   config->x11.display = v;
295  } else {
296   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k);
297  }
298 }
299
300 return 0;
301}
302
303struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) {
304 struct roar_libroar_config * config = roar_libroar_get_config();
305 return roar_libroar_config_codec_get_conf(codec, create, config);
306}
307
308static struct roar_libroar_config_codec *
309           roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config) {
310 int i;
311 int need_new = 1;
312
313 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
314
315 if ( codec < 0 || create < 0 )
316  return NULL;
317
318 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
319
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
333 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
334
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 ) {
349  config->codecs.codec = roar_mm_malloc(16*sizeof(struct roar_libroar_config_codec));
350 } else {
351  config->codecs.codec = roar_mm_realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec));
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
365 memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
366 config->codecs.codec[i].codec = codec;
367
368 return &(config->codecs.codec[i]);
369}
370
371int    roar_libroar_set_server(char * server) {
372 roar_libroar_get_config_ptr()->server = server;
373 return 0;
374}
375
376char * roar_libroar_get_server(void) {
377 return roar_libroar_get_config_ptr()->server;
378}
379
380void   roar_libroar_nowarn(void) {
381 roar_libroar_get_config_ptr()->nowarncounter++;
382}
383
384void   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
395//ll
Note: See TracBrowser for help on using the repository browser.