Ignore the listening angle for the wet path sound cones
[openal-soft.git] / Alc / alcConfig.c
blob6fc9db33bb8c4a15fb68beb98c3b2acda0067fe9
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 strncpy(curSection, section, sizeof(curSection)-1);
237 curSection[sizeof(curSection)-1] = 0;
240 continue;
243 comment = strchr(line, '#');
244 if(comment) *(comment++) = 0;
245 if(!line[0]) continue;
247 if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
248 sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
249 sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
251 /* sscanf doesn't handle '' or "" as empty values, so clip it
252 * manually. */
253 if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
254 value[0] = 0;
256 else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
258 /* Special case for 'key =' */
259 value[0] = 0;
261 else
263 ERR("config parse error: malformed option line: \"%s\"\n\n", line);
264 continue;
266 rstrip(key);
268 if(curSection[0] != 0)
270 size_t len = strlen(curSection);
271 memmove(&key[len+1], key, sizeof(key)-1-len);
272 key[len] = '/';
273 memcpy(key, curSection, len);
276 /* Check if we already have this option set */
277 ent = cfgBlock.entries;
278 while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
280 if(strcasecmp(ent->key, key) == 0)
281 break;
282 ent++;
285 if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
287 /* Allocate a new option entry */
288 ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
289 if(!ent)
291 ERR("config parse error: error reallocating config entries\n");
292 continue;
294 cfgBlock.entries = ent;
295 ent = cfgBlock.entries + cfgBlock.entryCount;
296 cfgBlock.entryCount++;
298 ent->key = strdup(key);
299 ent->value = NULL;
302 free(ent->value);
303 ent->value = expdup(value);
305 TRACE("found '%s' = '%s'\n", ent->key, ent->value);
308 free(buffer);
311 #ifdef _WIN32
312 void ReadALConfig(void)
314 WCHAR buffer[PATH_MAX];
315 const WCHAR *str;
316 FILE *f;
318 if(SHGetSpecialFolderPathW(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
320 al_string filepath = AL_STRING_INIT_STATIC();
321 al_string_copy_wcstr(&filepath, buffer);
322 al_string_append_cstr(&filepath, "\\alsoft.ini");
324 TRACE("Loading config %s...\n", al_string_get_cstr(filepath));
325 f = al_fopen(al_string_get_cstr(filepath), "rt");
326 if(f)
328 LoadConfigFromFile(f);
329 fclose(f);
331 al_string_deinit(&filepath);
334 if((str=_wgetenv(L"ALSOFT_CONF")) != NULL && *str)
336 al_string filepath = AL_STRING_INIT_STATIC();
337 al_string_copy_wcstr(&filepath, str);
339 TRACE("Loading config %s...\n", al_string_get_cstr(filepath));
340 f = al_fopen(al_string_get_cstr(filepath), "rt");
341 if(f)
343 LoadConfigFromFile(f);
344 fclose(f);
346 al_string_deinit(&filepath);
349 #else
350 void ReadALConfig(void)
352 char buffer[PATH_MAX];
353 const char *str;
354 FILE *f;
356 str = "/etc/openal/alsoft.conf";
358 TRACE("Loading config %s...\n", str);
359 f = al_fopen(str, "r");
360 if(f)
362 LoadConfigFromFile(f);
363 fclose(f);
366 if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
367 str = "/etc/xdg";
368 strncpy(buffer, str, sizeof(buffer)-1);
369 buffer[sizeof(buffer)-1] = 0;
370 /* Go through the list in reverse, since "the order of base directories
371 * denotes their importance; the first directory listed is the most
372 * important". Ergo, we need to load the settings from the later dirs
373 * first so that the settings in the earlier dirs override them.
375 while(1)
377 char *next = strrchr(buffer, ':');
378 if(next) *(next++) = 0;
379 else next = buffer;
381 if(next[0] != '/')
382 WARN("Ignoring XDG config dir: %s\n", next);
383 else
385 size_t len = strlen(next);
386 strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len);
387 buffer[sizeof(buffer)-1] = 0;
389 TRACE("Loading config %s...\n", next);
390 f = al_fopen(next, "r");
391 if(f)
393 LoadConfigFromFile(f);
394 fclose(f);
397 if(next == buffer)
398 break;
401 if((str=getenv("HOME")) != NULL && *str)
403 snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
405 TRACE("Loading config %s...\n", buffer);
406 f = al_fopen(buffer, "r");
407 if(f)
409 LoadConfigFromFile(f);
410 fclose(f);
414 if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
415 snprintf(buffer, sizeof(buffer), "%s/%s", str, "alsoft.conf");
416 else
418 buffer[0] = 0;
419 if((str=getenv("HOME")) != NULL && str[0] != 0)
420 snprintf(buffer, sizeof(buffer), "%s/.config/%s", str, "alsoft.conf");
422 if(buffer[0] != 0)
424 TRACE("Loading config %s...\n", buffer);
425 f = al_fopen(buffer, "r");
426 if(f)
428 LoadConfigFromFile(f);
429 fclose(f);
433 if((str=getenv("ALSOFT_CONF")) != NULL && *str)
435 TRACE("Loading config %s...\n", str);
436 f = al_fopen(str, "r");
437 if(f)
439 LoadConfigFromFile(f);
440 fclose(f);
444 #endif
446 void FreeALConfig(void)
448 unsigned int i;
450 for(i = 0;i < cfgBlock.entryCount;i++)
452 free(cfgBlock.entries[i].key);
453 free(cfgBlock.entries[i].value);
455 free(cfgBlock.entries);
458 const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def)
460 unsigned int i;
461 char key[256];
463 if(!keyName)
464 return def;
466 if(blockName && strcasecmp(blockName, "general") != 0)
468 if(devName)
469 snprintf(key, sizeof(key), "%s/%s/%s", blockName, devName, keyName);
470 else
471 snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
473 else
475 if(devName)
476 snprintf(key, sizeof(key), "%s/%s", devName, keyName);
477 else
479 strncpy(key, keyName, sizeof(key)-1);
480 key[sizeof(key)-1] = 0;
484 for(i = 0;i < cfgBlock.entryCount;i++)
486 if(strcmp(cfgBlock.entries[i].key, key) == 0)
488 TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
489 if(cfgBlock.entries[i].value[0])
490 return cfgBlock.entries[i].value;
491 return def;
495 if(!devName)
497 TRACE("Key %s not found\n", key);
498 return def;
500 return GetConfigValue(NULL, blockName, keyName, def);
503 int ConfigValueExists(const char *devName, const char *blockName, const char *keyName)
505 const char *val = GetConfigValue(devName, blockName, keyName, "");
506 return !!val[0];
509 int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret)
511 const char *val = GetConfigValue(devName, blockName, keyName, "");
512 if(!val[0]) return 0;
514 *ret = val;
515 return 1;
518 int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret)
520 const char *val = GetConfigValue(devName, blockName, keyName, "");
521 if(!val[0]) return 0;
523 *ret = strtol(val, NULL, 0);
524 return 1;
527 int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret)
529 const char *val = GetConfigValue(devName, blockName, keyName, "");
530 if(!val[0]) return 0;
532 *ret = strtoul(val, NULL, 0);
533 return 1;
536 int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret)
538 const char *val = GetConfigValue(devName, blockName, keyName, "");
539 if(!val[0]) return 0;
541 #ifdef HAVE_STRTOF
542 *ret = strtof(val, NULL);
543 #else
544 *ret = (float)strtod(val, NULL);
545 #endif
546 return 1;
549 int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret)
551 const char *val = GetConfigValue(devName, blockName, keyName, "");
552 if(!val[0]) return 0;
554 *ret = (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
555 strcasecmp(val, "on") == 0 || atoi(val) != 0);
556 return 1;
559 int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def)
561 const char *val = GetConfigValue(devName, blockName, keyName, "");
563 if(!val[0]) return !!def;
564 return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
565 strcasecmp(val, "on") == 0 || atoi(val) != 0);