source: roaraudio/libroar/config.c @ 4784:7aa703c721af

Last change on this file since 4784:7aa703c721af was 4784:7aa703c721af, checked in by phi, 13 years ago

added support for trap-policy:die and updated to a good message for policy:warn

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