source: roaraudio/libroar/config.c @ 2899:7a127e4f7f47

Last change on this file since 2899:7a127e4f7f47 was 2899:7a127e4f7f47, checked in by phi, 15 years ago

only set this in case no other server has allready been set

File size: 7.3 KB
Line 
1//config.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37static struct roar_libroar_config_codec *
38           roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config);
39
40
41struct roar_libroar_config * roar_libroar_get_config_ptr(void) {
42 static struct roar_libroar_config config;
43 static int inited = 0;
44
45 if ( !inited ) {
46  memset(&config, 0, sizeof(config));
47
48  config.server = NULL;
49
50  inited++;
51 }
52
53 return &config;
54}
55
56struct roar_libroar_config * roar_libroar_get_config(void) {
57 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
58 static int inited = 0;
59 char * next;
60
61 if ( !inited ) {
62  next = getenv("ROAR_OPTIONS");
63
64  if ( next != NULL ) {
65   roar_libroar_config_parse(next, " ");
66  }
67
68  inited++;
69 }
70
71 return config;
72}
73
74#define _P_FP(v)   ((int)(atof((v))*256.0))
75#define _P_INT(v)  (atoi((v)))
76#define _P_BOOL(v) (*(v) == 'y' || *(v) == 'j' || *(v) == 't' || *(v) == '1' ? 1 : 0)
77
78static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) {
79 struct roar_libroar_config_codec * codec_cfg;
80 int codec;
81 char * codec_str, * option_str, * value_str;
82
83 if ( config == NULL || txt == NULL )
84  return -1;
85
86 codec_str = strtok(txt, ":");
87
88 if ( codec_str == NULL )
89  return -1;
90
91 option_str = strtok(NULL, ":");
92
93 if ( option_str == NULL )
94  return -1;
95
96 value_str = strtok(NULL, ":");
97
98 if ( value_str == NULL )
99  return -1;
100
101 if ( (codec = roar_str2codec(codec_str)) == -1 ) {
102  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec: %s", codec_str);
103  return -1;
104 }
105
106 if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL )
107  return -1;
108
109 if ( !strcmp(option_str, "q") || !strcmp(option_str, "quality") ) {
110  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_Q;
111  codec_cfg->q = _P_FP(value_str);
112 } else if ( !strcmp(option_str, "complexity") ) {
113  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY;
114  codec_cfg->complexity = _P_FP(value_str);
115 } else if ( !strcmp(option_str, "dtx") ) {
116  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_DTX;
117  codec_cfg->dtx = _P_BOOL(value_str);
118 } else if ( !strcmp(option_str, "cc-max") ) {
119  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MAX_CC;
120  codec_cfg->max_cc = _P_INT(value_str);
121 } else {
122  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str);
123  return -1;
124 }
125
126 return 0;
127}
128
129int    roar_libroar_config_parse(char * txt, char * delm) {
130 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
131 char * k, * v, * next = txt;
132
133 while (next != NULL) {
134  k = next;
135
136  if ( delm == NULL ) {
137   // no delm -> we have only one option
138   next = NULL;
139  } else {
140   next = strstr(next, delm);
141  }
142
143  if ( next != NULL ) {
144   *next = 0;
145    next++;
146  }
147
148  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
149
150  // strip leading spaces:
151  while ( *k == ' ' ) k++;
152
153  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
154
155  // strip tailing new lions:
156  v = strtok(k, "\r\n");
157  if ( v != NULL ) {
158   if ( *v == '\r' || *v == '\n' )
159    *v = 0;
160  }
161
162  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
163
164  // comments
165  if ( *k == '#' )
166   continue;
167
168  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
169
170  // empty options:
171  if ( *k == 0 )
172   continue;
173
174  if ( (v = strstr(k, ":")) != NULL ) {
175   *v = 0;
176    v++;
177  }
178
179  ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v);
180
181  if ( !strcmp(k, "workaround") ) {
182   if ( !strcmp(v, "use-execed") ) {
183    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED;
184   } else {
185    ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v);
186   }
187  } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) {
188   if ( !strcmp(v, "sysio") ) {
189    config->warnings.sysio = ROAR_WARNING_ALWAYS;
190   } else {
191    ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v);
192   }
193  } else if ( !strcmp(k, "codec") ) {
194   if ( roar_libroar_config_parse_codec(config, v) == -1 ) {
195    ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option");
196   }
197  } else if ( !strcmp(k, "set-server") ) {
198   if ( roar_libroar_get_server() == NULL )
199    roar_libroar_set_server(v);
200  } else {
201   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k);
202  }
203 }
204
205 return 0;
206}
207
208struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) {
209 struct roar_libroar_config * config = roar_libroar_get_config();
210 return roar_libroar_config_codec_get_conf(codec, create, config);
211}
212
213static struct roar_libroar_config_codec *
214           roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config) {
215 int i;
216 int need_new = 1;
217
218 if ( codec < 0 || create < 0 )
219  return NULL;
220
221 if ( config->codecs.num == 0 ) {
222  // no match case:
223  if ( !create )
224   return NULL;
225 } else {
226  for (i = 0; i < config->codecs.num; i++) {
227   if ( config->codecs.codec[i].codec == codec )
228    return &(config->codecs.codec[i]);
229   if ( config->codecs.codec[i].codec == -1 )
230    need_new = 0;
231  }
232 }
233
234 if ( !create )
235  return NULL;
236
237 if ( !need_new ) {
238  for (i = 0; i < config->codecs.num; i++) {
239   if ( config->codecs.codec[i].codec == -1 ) {
240    memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
241    config->codecs.codec[i].codec = codec;
242    return &(config->codecs.codec[i]);
243   }
244  }
245 }
246
247 if ( config->codecs.num == 0 ) {
248  config->codecs.codec = malloc(16*sizeof(struct roar_libroar_config_codec));
249 } else {
250  config->codecs.codec = realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec));
251 }
252
253 if ( config->codecs.codec == NULL )
254  return NULL;
255
256 memset(&(config->codecs.codec[config->codecs.num]), 0, 16);
257 for (i = config->codecs.num; i < (config->codecs.num+16); i++) {
258  config->codecs.codec[i].codec = -1;
259 }
260
261 i = config->codecs.num;
262 config->codecs.num += 16;
263
264 return &(config->codecs.codec[i]);
265}
266
267int    roar_libroar_set_server(char * server) {
268 roar_libroar_get_config_ptr()->server = server;
269 return 0;
270}
271
272char * roar_libroar_get_server(void) {
273 return roar_libroar_get_config_ptr()->server;
274}
275
276//ll
Note: See TracBrowser for help on using the repository browser.