source: roaraudio/libroar/config.c @ 4779:64fbda1f79f8

Last change on this file since 4779:64fbda1f79f8 was 4779:64fbda1f79f8, checked in by phi, 13 years ago

Added support for a simple trap mechanism (pr1)

File size: 11.1 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 {
258    ROAR_WARN("roar_libroar_config_parse(*): Unknown trap policy: %s", v);
259   }
260#endif
261  } else if ( !strcmp(k, "force-rate") ) {
262   config->info.rate = atoi(v);
263  } else if ( !strcmp(k, "force-bits") ) {
264   config->info.bits = atoi(v);
265  } else if ( !strcmp(k, "force-channels") ) {
266   config->info.channels = atoi(v);
267  } else if ( !strcmp(k, "force-codec") ) {
268   config->info.codec = roar_str2codec(v);
269  } else if ( !strcmp(k, "codec") ) {
270   if ( roar_libroar_config_parse_codec(config, v) == -1 ) {
271    ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option");
272   }
273  } else if ( !strcmp(k, "set-server") ) {
274   if ( roar_libroar_get_server() == NULL )
275    roar_libroar_set_server(v);
276  } else if ( !strcmp(k, "set-authfile") ) {
277   strncpy(config->authfile, v, 1023);
278   config->authfile[1023] = 0;
279  } else if ( !strcmp(k, "x11-display") ) {
280   config->x11.display = v;
281  } else {
282   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k);
283  }
284 }
285
286 return 0;
287}
288
289struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) {
290 struct roar_libroar_config * config = roar_libroar_get_config();
291 return roar_libroar_config_codec_get_conf(codec, create, config);
292}
293
294static struct roar_libroar_config_codec *
295           roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config) {
296 int i;
297 int need_new = 1;
298
299 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
300
301 if ( codec < 0 || create < 0 )
302  return NULL;
303
304 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
305
306 if ( config->codecs.num == 0 ) {
307  // no match case:
308  if ( !create )
309   return NULL;
310 } else {
311  for (i = 0; i < config->codecs.num; i++) {
312   if ( config->codecs.codec[i].codec == codec )
313    return &(config->codecs.codec[i]);
314   if ( config->codecs.codec[i].codec == -1 )
315    need_new = 0;
316  }
317 }
318
319 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
320
321 if ( !create )
322  return NULL;
323
324 if ( !need_new ) {
325  for (i = 0; i < config->codecs.num; i++) {
326   if ( config->codecs.codec[i].codec == -1 ) {
327    memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
328    config->codecs.codec[i].codec = codec;
329    return &(config->codecs.codec[i]);
330   }
331  }
332 }
333
334 if ( config->codecs.num == 0 ) {
335  config->codecs.codec = malloc(16*sizeof(struct roar_libroar_config_codec));
336 } else {
337  config->codecs.codec = realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec));
338 }
339
340 if ( config->codecs.codec == NULL )
341  return NULL;
342
343 memset(&(config->codecs.codec[config->codecs.num]), 0, 16);
344 for (i = config->codecs.num; i < (config->codecs.num+16); i++) {
345  config->codecs.codec[i].codec = -1;
346 }
347
348 i = config->codecs.num;
349 config->codecs.num += 16;
350
351 memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
352 config->codecs.codec[i].codec = codec;
353
354 return &(config->codecs.codec[i]);
355}
356
357int    roar_libroar_set_server(char * server) {
358 roar_libroar_get_config_ptr()->server = server;
359 return 0;
360}
361
362char * roar_libroar_get_server(void) {
363 return roar_libroar_get_config_ptr()->server;
364}
365
366void   roar_libroar_nowarn(void) {
367 roar_libroar_get_config_ptr()->nowarncounter++;
368}
369
370void   roar_libroar_warn(void) {
371 struct roar_libroar_config * cfg = roar_libroar_get_config_ptr();
372
373 if ( cfg->nowarncounter == 0 ) {
374  ROAR_WARN("roar_libroar_warn(): Re-Enabling already enabled warnings! (Application error?)");
375  return;
376 }
377
378 cfg->nowarncounter--;
379}
380
381//ll
Note: See TracBrowser for help on using the repository browser.