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 | |
---|
38 | static struct roar_libroar_config_codec * |
---|
39 | roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config); |
---|
40 | |
---|
41 | |
---|
42 | struct 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 | |
---|
71 | struct 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 | next = getenv("ROAR_OPTIONS"); |
---|
78 | |
---|
79 | if ( next != NULL ) { |
---|
80 | roar_libroar_config_parse(next, " "); |
---|
81 | } |
---|
82 | |
---|
83 | inited++; |
---|
84 | } |
---|
85 | |
---|
86 | return config; |
---|
87 | } |
---|
88 | |
---|
89 | int 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 | |
---|
104 | static 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 | |
---|
183 | int 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 { |
---|
260 | ROAR_WARN("roar_libroar_config_parse(*): Unknown opmode: %s", v); |
---|
261 | } |
---|
262 | #ifdef ROAR_SUPPORT_TRAP |
---|
263 | } else if ( !strcmp(k, "trap-policy") ) { |
---|
264 | if ( !strcmp(v, "ignore") ) { |
---|
265 | config->trap_policy = ROAR_TRAP_IGNORE; |
---|
266 | } else if ( !strcmp(v, "warn") ) { |
---|
267 | config->trap_policy = ROAR_TRAP_WARN; |
---|
268 | } else if ( !strcmp(v, "abort") ) { |
---|
269 | config->trap_policy = ROAR_TRAP_ABORT; |
---|
270 | #ifdef SIGKILL |
---|
271 | } else if ( !strcmp(v, "kill") ) { |
---|
272 | config->trap_policy = ROAR_TRAP_KILL; |
---|
273 | #endif |
---|
274 | #ifdef SIGSTOP |
---|
275 | } else if ( !strcmp(v, "stop") ) { |
---|
276 | config->trap_policy = ROAR_TRAP_STOP; |
---|
277 | #endif |
---|
278 | } else if ( !strcmp(v, "die") ) { |
---|
279 | config->trap_policy = ROAR_TRAP_DIE; |
---|
280 | } else { |
---|
281 | ROAR_WARN("roar_libroar_config_parse(*): Unknown trap policy: %s", v); |
---|
282 | } |
---|
283 | #endif |
---|
284 | } else if ( !strcmp(k, "force-rate") ) { |
---|
285 | config->info.rate = atoi(v); |
---|
286 | } else if ( !strcmp(k, "force-bits") ) { |
---|
287 | config->info.bits = atoi(v); |
---|
288 | } else if ( !strcmp(k, "force-channels") ) { |
---|
289 | config->info.channels = atoi(v); |
---|
290 | } else if ( !strcmp(k, "force-codec") ) { |
---|
291 | config->info.codec = roar_str2codec(v); |
---|
292 | } else if ( !strcmp(k, "codec") ) { |
---|
293 | if ( roar_libroar_config_parse_codec(config, v) == -1 ) { |
---|
294 | ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option"); |
---|
295 | } |
---|
296 | } else if ( !strcmp(k, "set-server") ) { |
---|
297 | if ( roar_libroar_get_server() == NULL ) |
---|
298 | roar_libroar_set_server(v); |
---|
299 | } else if ( !strcmp(k, "set-authfile") ) { |
---|
300 | strncpy(config->authfile, v, 1023); |
---|
301 | config->authfile[1023] = 0; |
---|
302 | } else if ( !strcmp(k, "x11-display") ) { |
---|
303 | config->x11.display = v; |
---|
304 | } else { |
---|
305 | ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k); |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | return 0; |
---|
310 | } |
---|
311 | |
---|
312 | struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) { |
---|
313 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
314 | return roar_libroar_config_codec_get_conf(codec, create, config); |
---|
315 | } |
---|
316 | |
---|
317 | static struct roar_libroar_config_codec * |
---|
318 | roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config) { |
---|
319 | int i; |
---|
320 | int need_new = 1; |
---|
321 | |
---|
322 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
323 | |
---|
324 | if ( codec < 0 || create < 0 ) |
---|
325 | return NULL; |
---|
326 | |
---|
327 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
328 | |
---|
329 | if ( config->codecs.num == 0 ) { |
---|
330 | // no match case: |
---|
331 | if ( !create ) |
---|
332 | return NULL; |
---|
333 | } else { |
---|
334 | for (i = 0; i < config->codecs.num; i++) { |
---|
335 | if ( config->codecs.codec[i].codec == codec ) |
---|
336 | return &(config->codecs.codec[i]); |
---|
337 | if ( config->codecs.codec[i].codec == -1 ) |
---|
338 | need_new = 0; |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
343 | |
---|
344 | if ( !create ) |
---|
345 | return NULL; |
---|
346 | |
---|
347 | if ( !need_new ) { |
---|
348 | for (i = 0; i < config->codecs.num; i++) { |
---|
349 | if ( config->codecs.codec[i].codec == -1 ) { |
---|
350 | memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec)); |
---|
351 | config->codecs.codec[i].codec = codec; |
---|
352 | return &(config->codecs.codec[i]); |
---|
353 | } |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | if ( config->codecs.num == 0 ) { |
---|
358 | config->codecs.codec = roar_mm_malloc(16*sizeof(struct roar_libroar_config_codec)); |
---|
359 | } else { |
---|
360 | config->codecs.codec = roar_mm_realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec)); |
---|
361 | } |
---|
362 | |
---|
363 | if ( config->codecs.codec == NULL ) |
---|
364 | return NULL; |
---|
365 | |
---|
366 | memset(&(config->codecs.codec[config->codecs.num]), 0, 16); |
---|
367 | for (i = config->codecs.num; i < (config->codecs.num+16); i++) { |
---|
368 | config->codecs.codec[i].codec = -1; |
---|
369 | } |
---|
370 | |
---|
371 | i = config->codecs.num; |
---|
372 | config->codecs.num += 16; |
---|
373 | |
---|
374 | memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec)); |
---|
375 | config->codecs.codec[i].codec = codec; |
---|
376 | |
---|
377 | return &(config->codecs.codec[i]); |
---|
378 | } |
---|
379 | |
---|
380 | int roar_libroar_set_server(char * server) { |
---|
381 | roar_libroar_get_config_ptr()->server = server; |
---|
382 | return 0; |
---|
383 | } |
---|
384 | |
---|
385 | char * roar_libroar_get_server(void) { |
---|
386 | return roar_libroar_get_config_ptr()->server; |
---|
387 | } |
---|
388 | |
---|
389 | void roar_libroar_nowarn(void) { |
---|
390 | roar_libroar_get_config_ptr()->nowarncounter++; |
---|
391 | } |
---|
392 | |
---|
393 | void roar_libroar_warn(void) { |
---|
394 | struct roar_libroar_config * cfg = roar_libroar_get_config_ptr(); |
---|
395 | |
---|
396 | if ( cfg->nowarncounter == 0 ) { |
---|
397 | ROAR_WARN("roar_libroar_warn(): Re-Enabling already enabled warnings! (Application error?)"); |
---|
398 | return; |
---|
399 | } |
---|
400 | |
---|
401 | cfg->nowarncounter--; |
---|
402 | } |
---|
403 | |
---|
404 | //ll |
---|