source: roaraudio/libroar/config.c @ 5271:09aa484b25f4

Last change on this file since 5271:09aa484b25f4 was 5270:e25346c13638, checked in by phi, 12 years ago

fixed some gcc -Wextra warnings

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