source: roaraudio/libroar/config.c @ 5465:d5e7bc292531

Last change on this file since 5465:d5e7bc292531 was 5465:d5e7bc292531, checked in by phi, 12 years ago

no longer use fixed protocol version (_ROAR_MESSAGE_VERSION=0).
protocol version is now used by setting in message object.
If using connection based req (roar_req()) it will use version from connection
object setting. The connection object get's a default from the config.
The config key is "protocolversion".
roard will answer in the same version as the request was send in.

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