source: roaraudio/libroar/config.c @ 4517:203da07ed7d3

Last change on this file since 4517:203da07ed7d3 was 4517:203da07ed7d3, checked in by phi, 13 years ago

fixed const cast problem

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