source: roaraudio/libroar/config.c @ 3811:49db840fb4f4

Last change on this file since 3811:49db840fb4f4 was 3811:49db840fb4f4, checked in by phi, 14 years ago

fixed some copyright statements

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