source: roaraudio/libroar/config.c @ 4653:fb6662ea5f57

Last change on this file since 4653:fb6662ea5f57 was 4653:fb6662ea5f57, checked in by phi, 13 years ago

Support option to disable OpenSLP

File size: 10.5 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 if ( !strcmp(v, "no-slp") ) {
223    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP;
224   } else {
225    ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v);
226   }
227  } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) {
228   if ( !strcmp(v, "sysio") ) {
229    config->warnings.sysio = ROAR_WARNING_ALWAYS;
230   } else if ( !strcmp(v, "obsolete") ) {
231    config->warnings.obsolete = ROAR_WARNING_ALWAYS;
232   } else if ( !strcmp(v, "all") ) {
233    config->warnings.sysio    = ROAR_WARNING_ALWAYS;
234    config->warnings.obsolete = ROAR_WARNING_ALWAYS;
235   } else {
236    ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v);
237   }
238  } else if ( !strcmp(k, "force-rate") ) {
239   config->info.rate = atoi(v);
240  } else if ( !strcmp(k, "force-bits") ) {
241   config->info.bits = atoi(v);
242  } else if ( !strcmp(k, "force-channels") ) {
243   config->info.channels = atoi(v);
244  } else if ( !strcmp(k, "force-codec") ) {
245   config->info.codec = roar_str2codec(v);
246  } else if ( !strcmp(k, "codec") ) {
247   if ( roar_libroar_config_parse_codec(config, v) == -1 ) {
248    ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option");
249   }
250  } else if ( !strcmp(k, "set-server") ) {
251   if ( roar_libroar_get_server() == NULL )
252    roar_libroar_set_server(v);
253  } else if ( !strcmp(k, "set-authfile") ) {
254   strncpy(config->authfile, v, 1023);
255   config->authfile[1023] = 0;
256  } else if ( !strcmp(k, "x11-display") ) {
257   config->x11.display = v;
258  } else {
259   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k);
260  }
261 }
262
263 return 0;
264}
265
266struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) {
267 struct roar_libroar_config * config = roar_libroar_get_config();
268 return roar_libroar_config_codec_get_conf(codec, create, config);
269}
270
271static struct roar_libroar_config_codec *
272           roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config) {
273 int i;
274 int need_new = 1;
275
276 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
277
278 if ( codec < 0 || create < 0 )
279  return NULL;
280
281 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
282
283 if ( config->codecs.num == 0 ) {
284  // no match case:
285  if ( !create )
286   return NULL;
287 } else {
288  for (i = 0; i < config->codecs.num; i++) {
289   if ( config->codecs.codec[i].codec == codec )
290    return &(config->codecs.codec[i]);
291   if ( config->codecs.codec[i].codec == -1 )
292    need_new = 0;
293  }
294 }
295
296 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
297
298 if ( !create )
299  return NULL;
300
301 if ( !need_new ) {
302  for (i = 0; i < config->codecs.num; i++) {
303   if ( config->codecs.codec[i].codec == -1 ) {
304    memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
305    config->codecs.codec[i].codec = codec;
306    return &(config->codecs.codec[i]);
307   }
308  }
309 }
310
311 if ( config->codecs.num == 0 ) {
312  config->codecs.codec = malloc(16*sizeof(struct roar_libroar_config_codec));
313 } else {
314  config->codecs.codec = realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec));
315 }
316
317 if ( config->codecs.codec == NULL )
318  return NULL;
319
320 memset(&(config->codecs.codec[config->codecs.num]), 0, 16);
321 for (i = config->codecs.num; i < (config->codecs.num+16); i++) {
322  config->codecs.codec[i].codec = -1;
323 }
324
325 i = config->codecs.num;
326 config->codecs.num += 16;
327
328 memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
329 config->codecs.codec[i].codec = codec;
330
331 return &(config->codecs.codec[i]);
332}
333
334int    roar_libroar_set_server(char * server) {
335 roar_libroar_get_config_ptr()->server = server;
336 return 0;
337}
338
339char * roar_libroar_get_server(void) {
340 return roar_libroar_get_config_ptr()->server;
341}
342
343void   roar_libroar_nowarn(void) {
344 roar_libroar_get_config_ptr()->nowarncounter++;
345}
346
347void   roar_libroar_warn(void) {
348 struct roar_libroar_config * cfg = roar_libroar_get_config_ptr();
349
350 if ( cfg->nowarncounter == 0 ) {
351  ROAR_WARN("roar_libroar_warn(): Re-Enabling already enabled warnings! (Application error?)");
352  return;
353 }
354
355 cfg->nowarncounter--;
356}
357
358//ll
Note: See TracBrowser for help on using the repository browser.