Step mixing gains per-sample for non-HRTF mixing
[openal-soft.git] / Alc / alcConfig.c
blob740d0f869b1119d16f3fc69ed44c1905c10ff02b
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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>
36 #include "alMain.h"
38 #ifdef _WIN32_IE
39 #include <shlobj.h>
40 #endif
43 typedef struct ConfigEntry {
44 char *key;
45 char *value;
46 } ConfigEntry;
48 typedef struct ConfigBlock {
49 ConfigEntry *entries;
50 unsigned int entryCount;
51 } ConfigBlock;
52 static ConfigBlock cfgBlock;
55 static char *lstrip(char *line)
57 while(isspace(line[0]))
58 line++;
59 return line;
62 static char *rstrip(char *line)
64 size_t len = strlen(line);
65 while(len > 0 && isspace(line[len-1]))
66 len--;
67 line[len] = 0;
68 return line;
71 static int readline(FILE *f, char **output, size_t *maxlen)
73 size_t len = 0;
74 int c;
76 while((c=fgetc(f)) != EOF && (c == '\r' || c == '\n'))
78 if(c == EOF)
79 return 0;
81 do {
82 if(len+1 >= *maxlen)
84 void *temp = NULL;
85 size_t newmax;
87 newmax = (*maxlen ? (*maxlen)<<1 : 32);
88 if(newmax > *maxlen)
89 temp = realloc(*output, newmax);
90 if(!temp)
92 ERR("Failed to realloc %lu bytes from %lu!\n", newmax, *maxlen);
93 return 0;
96 *output = temp;
97 *maxlen = newmax;
99 (*output)[len++] = c;
100 (*output)[len] = '\0';
101 } while((c=fgetc(f)) != EOF && c != '\r' && c != '\n');
103 return 1;
107 static char *expdup(const char *str)
109 char *output = NULL;
110 size_t maxlen = 0;
111 size_t len = 0;
113 while(*str != '\0')
115 const char *addstr;
116 size_t addstrlen;
117 size_t i;
119 if(str[0] != '$')
121 const char *next = strchr(str, '$');
122 addstr = str;
123 addstrlen = next ? (size_t)(next-str) : strlen(str);
125 str += addstrlen;
127 else
129 str++;
130 if(*str == '$')
132 const char *next = strchr(str+1, '$');
133 addstr = str;
134 addstrlen = next ? (size_t)(next-str) : strlen(str);
136 str += addstrlen;
138 else
140 char envname[1024];
141 size_t k = 0;
143 while((isalnum(*str) || *str == '_') && k < sizeof(envname)-1)
144 envname[k++] = *(str++);
145 envname[k++] = '\0';
147 if((addstr=getenv(envname)) == NULL)
148 continue;
149 addstrlen = strlen(addstr);
152 if(addstrlen == 0)
153 continue;
155 if(addstrlen >= maxlen-len)
157 void *temp = NULL;
158 size_t newmax;
160 newmax = NextPowerOf2(len+addstrlen+1);
161 if(newmax > maxlen)
162 temp = realloc(output, newmax);
163 if(!temp)
165 ERR("Failed to realloc %lu bytes from %lu!\n", newmax, maxlen);
166 return output;
169 output = temp;
170 maxlen = newmax;
173 for(i = 0;i < addstrlen;i++)
174 output[len++] = addstr[i];
175 output[len] = '\0';
178 return output ? output : calloc(1, 1);
182 static void LoadConfigFromFile(FILE *f)
184 char curSection[128] = "";
185 char *buffer = NULL;
186 size_t maxlen = 0;
187 ConfigEntry *ent;
189 while(readline(f, &buffer, &maxlen))
191 char *line, *comment;
192 char key[256] = "";
193 char value[256] = "";
195 comment = strchr(buffer, '#');
196 if(comment)
198 *(comment++) = 0;
199 comment = rstrip(lstrip(comment));
202 line = rstrip(lstrip(buffer));
203 if(!line[0])
204 continue;
206 if(line[0] == '[')
208 char *section = line+1;
209 char *endsection;
211 endsection = strchr(section, ']');
212 if(!endsection || section == endsection || endsection[1] != 0)
214 ERR("config parse error: bad line \"%s\"\n", line);
215 continue;
217 *endsection = 0;
219 if(strcasecmp(section, "general") == 0)
220 curSection[0] = 0;
221 else
223 strncpy(curSection, section, sizeof(curSection)-1);
224 curSection[sizeof(curSection)-1] = 0;
227 continue;
230 if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
231 sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
232 sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
234 /* sscanf doesn't handle '' or "" as empty values, so clip it
235 * manually. */
236 if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
237 value[0] = 0;
239 else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
241 /* Special case for 'key =' */
242 value[0] = 0;
244 else
246 ERR("config parse error: malformed option line: \"%s\"\n\n", line);
247 continue;
249 rstrip(key);
251 if(curSection[0] != 0)
253 size_t len = strlen(curSection);
254 memmove(&key[len+1], key, sizeof(key)-1-len);
255 key[len] = '/';
256 memcpy(key, curSection, len);
259 /* Check if we already have this option set */
260 ent = cfgBlock.entries;
261 while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
263 if(strcasecmp(ent->key, key) == 0)
264 break;
265 ent++;
268 if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
270 /* Allocate a new option entry */
271 ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
272 if(!ent)
274 ERR("config parse error: error reallocating config entries\n");
275 continue;
277 cfgBlock.entries = ent;
278 ent = cfgBlock.entries + cfgBlock.entryCount;
279 cfgBlock.entryCount++;
281 ent->key = strdup(key);
282 ent->value = NULL;
285 free(ent->value);
286 ent->value = expdup(value);
288 TRACE("found '%s' = '%s'\n", ent->key, ent->value);
291 free(buffer);
294 void ReadALConfig(void)
296 char buffer[PATH_MAX];
297 const char *str;
298 FILE *f;
300 #ifdef _WIN32
301 if(SHGetSpecialFolderPathA(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
303 size_t p = strlen(buffer);
304 snprintf(buffer+p, sizeof(buffer)-p, "\\alsoft.ini");
306 TRACE("Loading config %s...\n", buffer);
307 f = fopen(buffer, "rt");
308 if(f)
310 LoadConfigFromFile(f);
311 fclose(f);
314 #else
315 str = "/etc/openal/alsoft.conf";
317 TRACE("Loading config %s...\n", str);
318 f = fopen(str, "r");
319 if(f)
321 LoadConfigFromFile(f);
322 fclose(f);
325 if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
326 str = "/etc/xdg";
327 strncpy(buffer, str, sizeof(buffer)-1);
328 buffer[sizeof(buffer)-1] = 0;
329 /* Go through the list in reverse, since "the order of base directories
330 * denotes their importance; the first directory listed is the most
331 * important". Ergo, we need to load the settings from the later dirs
332 * first so that the settings in the earlier dirs override them.
334 while(1)
336 char *next = strrchr(buffer, ':');
337 if(next) *(next++) = 0;
338 else next = buffer;
340 if(next[0] != '/')
341 WARN("Ignoring XDG config dir: %s\n", next);
342 else
344 size_t len = strlen(next);
345 strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len);
346 buffer[sizeof(buffer)-1] = 0;
348 TRACE("Loading config %s...\n", next);
349 f = fopen(next, "r");
350 if(f)
352 LoadConfigFromFile(f);
353 fclose(f);
356 if(next == buffer)
357 break;
360 if((str=getenv("HOME")) != NULL && *str)
362 snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
364 TRACE("Loading config %s...\n", buffer);
365 f = fopen(buffer, "r");
366 if(f)
368 LoadConfigFromFile(f);
369 fclose(f);
373 if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
374 snprintf(buffer, sizeof(buffer), "%s/%s", str, "alsoft.conf");
375 else
377 buffer[0] = 0;
378 if((str=getenv("HOME")) != NULL && str[0] != 0)
379 snprintf(buffer, sizeof(buffer), "%s/.config/%s", str, "alsoft.conf");
381 if(buffer[0] != 0)
383 TRACE("Loading config %s...\n", buffer);
384 f = fopen(buffer, "r");
385 if(f)
387 LoadConfigFromFile(f);
388 fclose(f);
391 #endif
393 if((str=getenv("ALSOFT_CONF")) != NULL && *str)
395 TRACE("Loading config %s...\n", str);
396 f = fopen(str, "r");
397 if(f)
399 LoadConfigFromFile(f);
400 fclose(f);
405 void FreeALConfig(void)
407 unsigned int i;
409 for(i = 0;i < cfgBlock.entryCount;i++)
411 free(cfgBlock.entries[i].key);
412 free(cfgBlock.entries[i].value);
414 free(cfgBlock.entries);
417 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
419 unsigned int i;
420 char key[256];
422 if(!keyName)
423 return def;
425 if(blockName && strcasecmp(blockName, "general") != 0)
426 snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
427 else
429 strncpy(key, keyName, sizeof(key)-1);
430 key[sizeof(key)-1] = 0;
433 for(i = 0;i < cfgBlock.entryCount;i++)
435 if(strcasecmp(cfgBlock.entries[i].key, key) == 0)
437 TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
438 if(cfgBlock.entries[i].value[0])
439 return cfgBlock.entries[i].value;
440 return def;
444 TRACE("Key %s not found\n", key);
445 return def;
448 int ConfigValueExists(const char *blockName, const char *keyName)
450 const char *val = GetConfigValue(blockName, keyName, "");
451 return !!val[0];
454 int ConfigValueStr(const char *blockName, const char *keyName, const char **ret)
456 const char *val = GetConfigValue(blockName, keyName, "");
457 if(!val[0]) return 0;
459 *ret = val;
460 return 1;
463 int ConfigValueInt(const char *blockName, const char *keyName, int *ret)
465 const char *val = GetConfigValue(blockName, keyName, "");
466 if(!val[0]) return 0;
468 *ret = strtol(val, NULL, 0);
469 return 1;
472 int ConfigValueUInt(const char *blockName, const char *keyName, unsigned int *ret)
474 const char *val = GetConfigValue(blockName, keyName, "");
475 if(!val[0]) return 0;
477 *ret = strtoul(val, NULL, 0);
478 return 1;
481 int ConfigValueFloat(const char *blockName, const char *keyName, float *ret)
483 const char *val = GetConfigValue(blockName, keyName, "");
484 if(!val[0]) return 0;
486 #ifdef HAVE_STRTOF
487 *ret = strtof(val, NULL);
488 #else
489 *ret = (float)strtod(val, NULL);
490 #endif
491 return 1;
494 int GetConfigValueBool(const char *blockName, const char *keyName, int def)
496 const char *val = GetConfigValue(blockName, keyName, "");
498 if(!val[0]) return !!def;
499 return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
500 strcasecmp(val, "on") == 0 || atoi(val) != 0);