source: roaraudio/libroar/config.c @ 3855:fbe1a88948e6

Last change on this file since 3855:fbe1a88948e6 was 3855:fbe1a88948e6, checked in by phi, 14 years ago

added config support for obsolete warnings, keyword 'all' and roar_libroar_*warn()

File size: 10.3 KB
Line 
1//config.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010
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 char        * home = getenv("HOME");
47
48 if ( !inited ) {
49  memset(&config, 0, sizeof(config));
50
51  config.server   = NULL;
52  config.authfile = NULL;
53
54  if ( home != NULL ) {
55   snprintf(authfile, 1023, "%s/.roarauth", home);
56   authfile[1023]     = 0;
57   config.authfile    = authfile;
58  }
59
60  inited++;
61 }
62
63 return &config;
64}
65
66struct roar_libroar_config * roar_libroar_get_config(void) {
67 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
68 static int inited = 0;
69 char * next;
70
71 if ( !inited ) {
72  next = getenv("ROAR_OPTIONS");
73
74  if ( next != NULL ) {
75   roar_libroar_config_parse(next, " ");
76  }
77
78  inited++;
79 }
80
81 return config;
82}
83
84#define _P_FP(v)   ((int)(atof((v))*256.0))
85#define _P_INT(v)  (atoi((v)))
86#define _P_BOOL(v) (*(v) == 'y' || *(v) == 'j' || *(v) == 't' || *(v) == '1' ? 1 : 0)
87
88static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) {
89 struct roar_libroar_config_codec * codec_cfg;
90 int codec;
91 char * codec_str, * option_str, * value_str;
92
93 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
94
95 if ( config == NULL || txt == NULL )
96  return -1;
97
98 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
99
100 codec_str = strtok(txt, ":");
101
102 if ( codec_str == NULL )
103  return -1;
104
105 option_str = strtok(NULL, ":");
106
107 if ( option_str == NULL )
108  return -1;
109
110 value_str = strtok(NULL, ":");
111
112 if ( value_str == NULL )
113  return -1;
114
115 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
116
117 if ( (codec = roar_str2codec(codec_str)) == -1 ) {
118  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec: %s", codec_str);
119  return -1;
120 }
121
122 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec);
123
124 if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL )
125  return -1;
126
127 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg);
128
129 if ( !strcmp(option_str, "q") || !strcmp(option_str, "quality") ) {
130  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_Q;
131  codec_cfg->q = _P_FP(value_str);
132 } else if ( !strcmp(option_str, "complexity") ) {
133  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY;
134  codec_cfg->complexity = _P_FP(value_str);
135 } else if ( !strcmp(option_str, "dtx") ) {
136  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_DTX;
137  codec_cfg->dtx = _P_BOOL(value_str);
138 } else if ( !strcmp(option_str, "cc-max") ) {
139  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MAX_CC;
140  codec_cfg->max_cc = _P_INT(value_str);
141 } else if ( !strcmp(option_str, "vbr") ) {
142  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_VBR;
143  codec_cfg->vbr = _P_BOOL(value_str);
144 } else if ( !strcmp(option_str, "mode") ) {
145  if ( !strcmp(value_str, "nb") ) {
146   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
147   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_NB;
148  } else if ( !strcmp(value_str, "wb") ) {
149   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
150   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_WB;
151  } else if ( !strcmp(value_str, "uwb") ) {
152   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
153   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_UWB;
154  } else {
155   ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec mode: %s", value_str);
156   return -1;
157  }
158 } else {
159  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str);
160  return -1;
161 }
162
163 return 0;
164}
165
166int    roar_libroar_config_parse(char * txt, char * delm) {
167 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
168 char * k, * v, * next = txt;
169
170 while (next != NULL) {
171  k = next;
172
173  if ( delm == NULL ) {
174   // no delm -> we have only one option
175   next = NULL;
176  } else {
177   next = strstr(next, delm);
178  }
179
180  if ( next != NULL ) {
181   *next = 0;
182    next++;
183  }
184
185  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
186
187  // strip leading spaces:
188  while ( *k == ' ' ) k++;
189
190  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
191
192  // strip tailing new lions:
193  v = strtok(k, "\r\n");
194  if ( v != NULL ) {
195   if ( *v == '\r' || *v == '\n' )
196    *v = 0;
197  }
198
199  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
200
201  // comments
202  if ( *k == '#' )
203   continue;
204
205  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
206
207  // empty options:
208  if ( *k == 0 )
209   continue;
210
211  if ( (v = strstr(k, ":")) != NULL ) {
212   *v = 0;
213    v++;
214  }
215
216  ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v);
217
218  if ( !strcmp(k, "workaround") ) {
219   if ( !strcmp(v, "use-execed") ) {
220    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED;
221   } else {
222    ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v);
223   }
224  } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) {
225   if ( !strcmp(v, "sysio") ) {
226    config->warnings.sysio = ROAR_WARNING_ALWAYS;
227   } else if ( !strcmp(v, "obsolete") ) {
228    config->warnings.obsolete = ROAR_WARNING_ALWAYS;
229   } else if ( !strcmp(v, "all") ) {
230    config->warnings.sysio    = ROAR_WARNING_ALWAYS;
231    config->warnings.obsolete = ROAR_WARNING_ALWAYS;
232   } else {
233    ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v);
234   }
235  } else if ( !strcmp(k, "force-rate") ) {
236   config->info.rate = atoi(v);
237  } else if ( !strcmp(k, "force-bits") ) {
238   config->info.bits = atoi(v);
239  } else if ( !strcmp(k, "force-channels") ) {
240   config->info.channels = atoi(v);
241  } else if ( !strcmp(k, "force-codec") ) {
242   config->info.codec = roar_str2codec(v);
243  } else if ( !strcmp(k, "codec") ) {
244   if ( roar_libroar_config_parse_codec(config, v) == -1 ) {
245    ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option");
246   }
247  } else if ( !strcmp(k, "set-server") ) {
248   if ( roar_libroar_get_server() == NULL )
249    roar_libroar_set_server(v);
250  } else if ( !strcmp(k, "set-authfile") ) {
251   strncpy(config->authfile, v, 1023);
252   config->authfile[1023] = 0;
253  } else if ( !strcmp(k, "x11-display") ) {
254   config->x11.display = v;
255  } else {
256   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k);
257  }
258 }
259
260 return 0;
261}
262
263struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) {
264 struct roar_libroar_config * config = roar_libroar_get_config();
265 return roar_libroar_config_codec_get_conf(codec, create, config);
266}
267
268static struct roar_libroar_config_codec *
269           roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config) {
270 int i;
271 int need_new = 1;
272
273 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
274
275 if ( codec < 0 || create < 0 )
276  return NULL;
277
278 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
279
280 if ( config->codecs.num == 0 ) {
281  // no match case:
282  if ( !create )
283   return NULL;
284 } else {
285  for (i = 0; i < config->codecs.num; i++) {
286   if ( config->codecs.codec[i].codec == codec )
287    return &(config->codecs.codec[i]);
288   if ( config->codecs.codec[i].codec == -1 )
289    need_new = 0;
290  }
291 }
292
293 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
294
295 if ( !create )
296  return NULL;
297
298 if ( !need_new ) {
299  for (i = 0; i < config->codecs.num; i++) {
300   if ( config->codecs.codec[i].codec == -1 ) {
301    memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
302    config->codecs.codec[i].codec = codec;
303    return &(config->codecs.codec[i]);
304   }
305  }
306 }
307
308 if ( config->codecs.num == 0 ) {
309  config->codecs.codec = malloc(16*sizeof(struct roar_libroar_config_codec));
310 } else {
311  config->codecs.codec = realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec));
312 }
313
314 if ( config->codecs.codec == NULL )
315  return NULL;
316
317 memset(&(config->codecs.codec[config->codecs.num]), 0, 16);
318 for (i = config->codecs.num; i < (config->codecs.num+16); i++) {
319  config->codecs.codec[i].codec = -1;
320 }
321
322 i = config->codecs.num;
323 config->codecs.num += 16;
324
325 memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
326 config->codecs.codec[i].codec = codec;
327
328 return &(config->codecs.codec[i]);
329}
330
331int    roar_libroar_set_server(char * server) {
332 roar_libroar_get_config_ptr()->server = server;
333 return 0;
334}
335
336char * roar_libroar_get_server(void) {
337 return roar_libroar_get_config_ptr()->server;
338}
339
340void   roar_libroar_nowarn(void) {
341 roar_libroar_get_config_ptr()->nowarncounter++;
342}
343
344void   roar_libroar_warn(void) {
345 struct roar_libroar_config * cfg = roar_libroar_get_config_ptr();
346
347 if ( cfg->nowarncounter == 0 ) {
348  ROAR_WARN("roar_libroar_warn(): Re-Enabling already enabled warnings! (Application error?)");
349  return;
350 }
351
352 cfg->nowarncounter--;
353}
354
355//ll
Note: See TracBrowser for help on using the repository browser.