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