Apply reverb modulation to the late feedback lines
[openal-soft.git] / Alc / alcConfig.c
blob6fd0174610387edc3240ee3f8dbb12bc2ff39a53
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #ifdef _WIN32
22 #ifdef __MINGW32__
23 #define _WIN32_IE 0x501
24 #else
25 #define _WIN32_IE 0x400
26 #endif
27 #endif
29 #include "config.h"
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <string.h>
35 #ifdef _WIN32_IE
36 #include <windows.h>
37 #include <shlobj.h>
38 #endif
40 #include "alMain.h"
41 #include "compat.h"
42 #include "bool.h"
45 typedef struct ConfigEntry {
46 char *key;
47 char *value;
48 } ConfigEntry;
50 typedef struct ConfigBlock {
51 ConfigEntry *entries;
52 unsigned int entryCount;
53 } ConfigBlock;
54 static ConfigBlock cfgBlock;
57 static char *lstrip(char *line)
59 while(isspace(line[0]))
60 line++;
61 return line;
64 static char *rstrip(char *line)
66 size_t len = strlen(line);
67 while(len > 0 && isspace(line[len-1]))
68 len--;
69 line[len] = 0;
70 return line;
73 static int readline(FILE *f, char **output, size_t *maxlen)
75 size_t len = 0;
76 int c;
78 while((c=fgetc(f)) != EOF && (c == '\r' || c == '\n'))
80 if(c == EOF)
81 return 0;
83 do {
84 if(len+1 >= *maxlen)
86 void *temp = NULL;
87 size_t newmax;
89 newmax = (*maxlen ? (*maxlen)<<1 : 32);
90 if(newmax > *maxlen)
91 temp = realloc(*output, newmax);
92 if(!temp)
94 ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, *maxlen);
95 return 0;
98 *output = temp;
99 *maxlen = newmax;
101 (*output)[len++] = c;
102 (*output)[len] = '\0';
103 } while((c=fgetc(f)) != EOF && c != '\r' && c != '\n');
105 return 1;
109 static char *expdup(const char *str)
111 char *output = NULL;
112 size_t maxlen = 0;
113 size_t len = 0;
115 while(*str != '\0')
117 const char *addstr;
118 size_t addstrlen;
119 size_t i;
121 if(str[0] != '$')
123 const char *next = strchr(str, '$');
124 addstr = str;
125 addstrlen = next ? (size_t)(next-str) : strlen(str);
127 str += addstrlen;
129 else
131 str++;
132 if(*str == '$')
134 const char *next = strchr(str+1, '$');
135 addstr = str;
136 addstrlen = next ? (size_t)(next-str) : strlen(str);
138 str += addstrlen;
140 else
142 bool hasbraces;
143 char envname[1024];
144 size_t k = 0;
146 hasbraces = (*str == '{');
147 if(hasbraces) str++;
149 while((isalnum(*str) || *str == '_') && k < sizeof(envname)-1)
150 envname[k++] = *(str++);
151 envname[k++] = '\0';
153 if(hasbraces && *str != '}')
154 continue;
156 if(hasbraces) str++;
157 if((addstr=getenv(envname)) == NULL)
158 continue;
159 addstrlen = strlen(addstr);
162 if(addstrlen == 0)
163 continue;
165 if(addstrlen >= maxlen-len)
167 void *temp = NULL;
168 size_t newmax;
170 newmax = len+addstrlen+1;
171 if(newmax > maxlen)
172 temp = realloc(output, newmax);
173 if(!temp)
175 ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, maxlen);
176 return output;
179 output = temp;
180 maxlen = newmax;
183 for(i = 0;i < addstrlen;i++)
184 output[len++] = addstr[i];
185 output[len] = '\0';
188 return output ? output : calloc(1, 1);
192 static void LoadConfigFromFile(FILE *f)
194 char curSection[128] = "";
195 char *buffer = NULL;
196 size_t maxlen = 0;
197 ConfigEntry *ent;
199 while(readline(f, &buffer, &maxlen))
201 char *line, *comment;
202 char key[256] = "";
203 char value[256] = "";
205 line = rstrip(lstrip(buffer));
206 if(!line[0]) continue;
208 if(line[0] == '[')
210 char *section = line+1;
211 char *endsection;
213 endsection = strchr(section, ']');
214 if(!endsection || section == endsection)
216 ERR("config parse error: bad line \"%s\"\n", line);
217 continue;
219 if(endsection[1] != 0)
221 char *end = endsection+1;
222 while(isspace(*end))
223 ++end;
224 if(*end != 0 && *end != '#')
226 ERR("config parse error: bad line \"%s\"\n", line);
227 continue;
230 *endsection = 0;
232 if(strcasecmp(section, "general") == 0)
233 curSection[0] = 0;
234 else
236 size_t len, p = 0;
237 do {
238 char *nextp = strchr(section, '%');
239 if(!nextp)
241 strncpy(curSection+p, section, sizeof(curSection)-1-p);
242 break;
245 len = nextp - section;
246 if(len > sizeof(curSection)-1-p)
247 len = sizeof(curSection)-1-p;
248 strncpy(curSection+p, section, len);
249 p += len;
250 section = nextp;
252 if(((section[1] >= '0' && section[1] <= '9') ||
253 (section[1] >= 'a' && section[1] <= 'f') ||
254 (section[1] >= 'A' && section[1] <= 'F')) &&
255 ((section[2] >= '0' && section[2] <= '9') ||
256 (section[2] >= 'a' && section[2] <= 'f') ||
257 (section[2] >= 'A' && section[2] <= 'F')))
259 unsigned char b = 0;
260 if(section[1] >= '0' && section[1] <= '9')
261 b = (section[1]-'0') << 4;
262 else if(section[1] >= 'a' && section[1] <= 'f')
263 b = (section[1]-'a'+0xa) << 4;
264 else if(section[1] >= 'A' && section[1] <= 'F')
265 b = (section[1]-'A'+0x0a) << 4;
266 if(section[2] >= '0' && section[2] <= '9')
267 b |= (section[2]-'0');
268 else if(section[2] >= 'a' && section[2] <= 'f')
269 b |= (section[2]-'a'+0xa);
270 else if(section[2] >= 'A' && section[2] <= 'F')
271 b |= (section[2]-'A'+0x0a);
272 if(p < sizeof(curSection)-1)
273 curSection[p++] = b;
274 section += 3;
276 else if(section[1] == '%')
278 if(p < sizeof(curSection)-1)
279 curSection[p++] = '%';
280 section += 2;
282 else
284 if(p < sizeof(curSection)-1)
285 curSection[p++] = '%';
286 section += 1;
288 if(p < sizeof(curSection)-1)
289 curSection[p] = 0;
290 } while(p < sizeof(curSection)-1 && *section != 0);
291 curSection[sizeof(curSection)-1] = 0;
294 continue;
297 comment = strchr(line, '#');
298 if(comment) *(comment++) = 0;
299 if(!line[0]) continue;
301 if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
302 sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
303 sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
305 /* sscanf doesn't handle '' or "" as empty values, so clip it
306 * manually. */
307 if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
308 value[0] = 0;
310 else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
312 /* Special case for 'key =' */
313 value[0] = 0;
315 else
317 ERR("config parse error: malformed option line: \"%s\"\n\n", line);
318 continue;
320 rstrip(key);
322 if(curSection[0] != 0)
324 size_t len = strlen(curSection);
325 memmove(&key[len+1], key, sizeof(key)-1-len);
326 key[len] = '/';
327 memcpy(key, curSection, len);
330 /* Check if we already have this option set */
331 ent = cfgBlock.entries;
332 while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
334 if(strcasecmp(ent->key, key) == 0)
335 break;
336 ent++;
339 if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
341 /* Allocate a new option entry */
342 ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
343 if(!ent)
345 ERR("config parse error: error reallocating config entries\n");
346 continue;
348 cfgBlock.entries = ent;
349 ent = cfgBlock.entries + cfgBlock.entryCount;
350 cfgBlock.entryCount++;
352 ent->key = strdup(key);
353 ent->value = NULL;
356 free(ent->value);
357 ent->value = expdup(value);
359 TRACE("found '%s' = '%s'\n", ent->key, ent->value);
362 free(buffer);
365 #ifdef _WIN32
366 void ReadALConfig(void)
368 WCHAR buffer[PATH_MAX];
369 const WCHAR *str;
370 al_string ppath;
371 FILE *f;
373 if(SHGetSpecialFolderPathW(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
375 al_string filepath = AL_STRING_INIT_STATIC();
376 alstr_copy_wcstr(&filepath, buffer);
377 alstr_append_cstr(&filepath, "\\alsoft.ini");
379 TRACE("Loading config %s...\n", alstr_get_cstr(filepath));
380 f = al_fopen(alstr_get_cstr(filepath), "rt");
381 if(f)
383 LoadConfigFromFile(f);
384 fclose(f);
386 alstr_reset(&filepath);
389 ppath = GetProcPath();
390 if(!alstr_empty(ppath))
392 alstr_append_cstr(&ppath, "\\alsoft.ini");
393 TRACE("Loading config %s...\n", alstr_get_cstr(ppath));
394 f = al_fopen(alstr_get_cstr(ppath), "r");
395 if(f)
397 LoadConfigFromFile(f);
398 fclose(f);
402 if((str=_wgetenv(L"ALSOFT_CONF")) != NULL && *str)
404 al_string filepath = AL_STRING_INIT_STATIC();
405 alstr_copy_wcstr(&filepath, str);
407 TRACE("Loading config %s...\n", alstr_get_cstr(filepath));
408 f = al_fopen(alstr_get_cstr(filepath), "rt");
409 if(f)
411 LoadConfigFromFile(f);
412 fclose(f);
414 alstr_reset(&filepath);
417 alstr_reset(&ppath);
419 #else
420 void ReadALConfig(void)
422 char buffer[PATH_MAX];
423 const char *str;
424 al_string ppath;
425 FILE *f;
427 str = "/etc/openal/alsoft.conf";
429 TRACE("Loading config %s...\n", str);
430 f = al_fopen(str, "r");
431 if(f)
433 LoadConfigFromFile(f);
434 fclose(f);
437 if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
438 str = "/etc/xdg";
439 strncpy(buffer, str, sizeof(buffer)-1);
440 buffer[sizeof(buffer)-1] = 0;
441 /* Go through the list in reverse, since "the order of base directories
442 * denotes their importance; the first directory listed is the most
443 * important". Ergo, we need to load the settings from the later dirs
444 * first so that the settings in the earlier dirs override them.
446 while(1)
448 char *next = strrchr(buffer, ':');
449 if(next) *(next++) = 0;
450 else next = buffer;
452 if(next[0] != '/')
453 WARN("Ignoring XDG config dir: %s\n", next);
454 else
456 size_t len = strlen(next);
457 strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len);
458 buffer[sizeof(buffer)-1] = 0;
460 TRACE("Loading config %s...\n", next);
461 f = al_fopen(next, "r");
462 if(f)
464 LoadConfigFromFile(f);
465 fclose(f);
468 if(next == buffer)
469 break;
472 if((str=getenv("HOME")) != NULL && *str)
474 snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
476 TRACE("Loading config %s...\n", buffer);
477 f = al_fopen(buffer, "r");
478 if(f)
480 LoadConfigFromFile(f);
481 fclose(f);
485 if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
486 snprintf(buffer, sizeof(buffer), "%s/%s", str, "alsoft.conf");
487 else
489 buffer[0] = 0;
490 if((str=getenv("HOME")) != NULL && str[0] != 0)
491 snprintf(buffer, sizeof(buffer), "%s/.config/%s", str, "alsoft.conf");
493 if(buffer[0] != 0)
495 TRACE("Loading config %s...\n", buffer);
496 f = al_fopen(buffer, "r");
497 if(f)
499 LoadConfigFromFile(f);
500 fclose(f);
504 ppath = GetProcPath();
505 if(!alstr_empty(ppath))
507 alstr_append_cstr(&ppath, "/alsoft.conf");
508 TRACE("Loading config %s...\n", alstr_get_cstr(ppath));
509 f = al_fopen(alstr_get_cstr(ppath), "r");
510 if(f)
512 LoadConfigFromFile(f);
513 fclose(f);
517 if((str=getenv("ALSOFT_CONF")) != NULL && *str)
519 TRACE("Loading config %s...\n", str);
520 f = al_fopen(str, "r");
521 if(f)
523 LoadConfigFromFile(f);
524 fclose(f);
528 alstr_reset(&ppath);
530 #endif
532 void FreeALConfig(void)
534 unsigned int i;
536 for(i = 0;i < cfgBlock.entryCount;i++)
538 free(cfgBlock.entries[i].key);
539 free(cfgBlock.entries[i].value);
541 free(cfgBlock.entries);
544 const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def)
546 unsigned int i;
547 char key[256];
549 if(!keyName)
550 return def;
552 if(blockName && strcasecmp(blockName, "general") != 0)
554 if(devName)
555 snprintf(key, sizeof(key), "%s/%s/%s", blockName, devName, keyName);
556 else
557 snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
559 else
561 if(devName)
562 snprintf(key, sizeof(key), "%s/%s", devName, keyName);
563 else
565 strncpy(key, keyName, sizeof(key)-1);
566 key[sizeof(key)-1] = 0;
570 for(i = 0;i < cfgBlock.entryCount;i++)
572 if(strcmp(cfgBlock.entries[i].key, key) == 0)
574 TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
575 if(cfgBlock.entries[i].value[0])
576 return cfgBlock.entries[i].value;
577 return def;
581 if(!devName)
583 TRACE("Key %s not found\n", key);
584 return def;
586 return GetConfigValue(NULL, blockName, keyName, def);
589 int ConfigValueExists(const char *devName, const char *blockName, const char *keyName)
591 const char *val = GetConfigValue(devName, blockName, keyName, "");
592 return !!val[0];
595 int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret)
597 const char *val = GetConfigValue(devName, blockName, keyName, "");
598 if(!val[0]) return 0;
600 *ret = val;
601 return 1;
604 int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret)
606 const char *val = GetConfigValue(devName, blockName, keyName, "");
607 if(!val[0]) return 0;
609 *ret = strtol(val, NULL, 0);
610 return 1;
613 int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret)
615 const char *val = GetConfigValue(devName, blockName, keyName, "");
616 if(!val[0]) return 0;
618 *ret = strtoul(val, NULL, 0);
619 return 1;
622 int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret)
624 const char *val = GetConfigValue(devName, blockName, keyName, "");
625 if(!val[0]) return 0;
627 #ifdef HAVE_STRTOF
628 *ret = strtof(val, NULL);
629 #else
630 *ret = (float)strtod(val, NULL);
631 #endif
632 return 1;
635 int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret)
637 const char *val = GetConfigValue(devName, blockName, keyName, "");
638 if(!val[0]) return 0;
640 *ret = (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
641 strcasecmp(val, "on") == 0 || atoi(val) != 0);
642 return 1;
645 int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def)
647 const char *val = GetConfigValue(devName, blockName, keyName, "");
649 if(!val[0]) return !!def;
650 return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
651 strcasecmp(val, "on") == 0 || atoi(val) != 0);