Request the device's sample type for SDL2
[openal-soft.git] / Alc / alconfig.c
blob5dbf59d2bb760066b49786d82c11bc816ddcb356
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 "alconfig.h"
42 #include "compat.h"
43 #include "bool.h"
46 typedef struct ConfigEntry {
47 char *key;
48 char *value;
49 } ConfigEntry;
51 typedef struct ConfigBlock {
52 ConfigEntry *entries;
53 unsigned int entryCount;
54 } ConfigBlock;
55 static ConfigBlock cfgBlock;
58 static char *lstrip(char *line)
60 while(isspace(line[0]))
61 line++;
62 return line;
65 static char *rstrip(char *line)
67 size_t len = strlen(line);
68 while(len > 0 && isspace(line[len-1]))
69 len--;
70 line[len] = 0;
71 return line;
74 static int readline(FILE *f, char **output, size_t *maxlen)
76 size_t len = 0;
77 int c;
79 while((c=fgetc(f)) != EOF && (c == '\r' || c == '\n'))
81 if(c == EOF)
82 return 0;
84 do {
85 if(len+1 >= *maxlen)
87 void *temp = NULL;
88 size_t newmax;
90 newmax = (*maxlen ? (*maxlen)<<1 : 32);
91 if(newmax > *maxlen)
92 temp = realloc(*output, newmax);
93 if(!temp)
95 ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, *maxlen);
96 return 0;
99 *output = temp;
100 *maxlen = newmax;
102 (*output)[len++] = c;
103 (*output)[len] = '\0';
104 } while((c=fgetc(f)) != EOF && c != '\r' && c != '\n');
106 return 1;
110 static char *expdup(const char *str)
112 char *output = NULL;
113 size_t maxlen = 0;
114 size_t len = 0;
116 while(*str != '\0')
118 const char *addstr;
119 size_t addstrlen;
120 size_t i;
122 if(str[0] != '$')
124 const char *next = strchr(str, '$');
125 addstr = str;
126 addstrlen = next ? (size_t)(next-str) : strlen(str);
128 str += addstrlen;
130 else
132 str++;
133 if(*str == '$')
135 const char *next = strchr(str+1, '$');
136 addstr = str;
137 addstrlen = next ? (size_t)(next-str) : strlen(str);
139 str += addstrlen;
141 else
143 bool hasbraces;
144 char envname[1024];
145 size_t k = 0;
147 hasbraces = (*str == '{');
148 if(hasbraces) str++;
150 while((isalnum(*str) || *str == '_') && k < sizeof(envname)-1)
151 envname[k++] = *(str++);
152 envname[k++] = '\0';
154 if(hasbraces && *str != '}')
155 continue;
157 if(hasbraces) str++;
158 if((addstr=getenv(envname)) == NULL)
159 continue;
160 addstrlen = strlen(addstr);
163 if(addstrlen == 0)
164 continue;
166 if(addstrlen >= maxlen-len)
168 void *temp = NULL;
169 size_t newmax;
171 newmax = len+addstrlen+1;
172 if(newmax > maxlen)
173 temp = realloc(output, newmax);
174 if(!temp)
176 ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, maxlen);
177 return output;
180 output = temp;
181 maxlen = newmax;
184 for(i = 0;i < addstrlen;i++)
185 output[len++] = addstr[i];
186 output[len] = '\0';
189 return output ? output : calloc(1, 1);
193 static void LoadConfigFromFile(FILE *f)
195 char curSection[128] = "";
196 char *buffer = NULL;
197 size_t maxlen = 0;
198 ConfigEntry *ent;
200 while(readline(f, &buffer, &maxlen))
202 char *line, *comment;
203 char key[256] = "";
204 char value[256] = "";
206 line = rstrip(lstrip(buffer));
207 if(!line[0]) continue;
209 if(line[0] == '[')
211 char *section = line+1;
212 char *endsection;
214 endsection = strchr(section, ']');
215 if(!endsection || section == endsection)
217 ERR("config parse error: bad line \"%s\"\n", line);
218 continue;
220 if(endsection[1] != 0)
222 char *end = endsection+1;
223 while(isspace(*end))
224 ++end;
225 if(*end != 0 && *end != '#')
227 ERR("config parse error: bad line \"%s\"\n", line);
228 continue;
231 *endsection = 0;
233 if(strcasecmp(section, "general") == 0)
234 curSection[0] = 0;
235 else
237 size_t len, p = 0;
238 do {
239 char *nextp = strchr(section, '%');
240 if(!nextp)
242 strncpy(curSection+p, section, sizeof(curSection)-1-p);
243 break;
246 len = nextp - section;
247 if(len > sizeof(curSection)-1-p)
248 len = sizeof(curSection)-1-p;
249 strncpy(curSection+p, section, len);
250 p += len;
251 section = nextp;
253 if(((section[1] >= '0' && section[1] <= '9') ||
254 (section[1] >= 'a' && section[1] <= 'f') ||
255 (section[1] >= 'A' && section[1] <= 'F')) &&
256 ((section[2] >= '0' && section[2] <= '9') ||
257 (section[2] >= 'a' && section[2] <= 'f') ||
258 (section[2] >= 'A' && section[2] <= 'F')))
260 unsigned char b = 0;
261 if(section[1] >= '0' && section[1] <= '9')
262 b = (section[1]-'0') << 4;
263 else if(section[1] >= 'a' && section[1] <= 'f')
264 b = (section[1]-'a'+0xa) << 4;
265 else if(section[1] >= 'A' && section[1] <= 'F')
266 b = (section[1]-'A'+0x0a) << 4;
267 if(section[2] >= '0' && section[2] <= '9')
268 b |= (section[2]-'0');
269 else if(section[2] >= 'a' && section[2] <= 'f')
270 b |= (section[2]-'a'+0xa);
271 else if(section[2] >= 'A' && section[2] <= 'F')
272 b |= (section[2]-'A'+0x0a);
273 if(p < sizeof(curSection)-1)
274 curSection[p++] = b;
275 section += 3;
277 else if(section[1] == '%')
279 if(p < sizeof(curSection)-1)
280 curSection[p++] = '%';
281 section += 2;
283 else
285 if(p < sizeof(curSection)-1)
286 curSection[p++] = '%';
287 section += 1;
289 if(p < sizeof(curSection)-1)
290 curSection[p] = 0;
291 } while(p < sizeof(curSection)-1 && *section != 0);
292 curSection[sizeof(curSection)-1] = 0;
295 continue;
298 comment = strchr(line, '#');
299 if(comment) *(comment++) = 0;
300 if(!line[0]) continue;
302 if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
303 sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
304 sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
306 /* sscanf doesn't handle '' or "" as empty values, so clip it
307 * manually. */
308 if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
309 value[0] = 0;
311 else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
313 /* Special case for 'key =' */
314 value[0] = 0;
316 else
318 ERR("config parse error: malformed option line: \"%s\"\n\n", line);
319 continue;
321 rstrip(key);
323 if(curSection[0] != 0)
325 size_t len = strlen(curSection);
326 memmove(&key[len+1], key, sizeof(key)-1-len);
327 key[len] = '/';
328 memcpy(key, curSection, len);
331 /* Check if we already have this option set */
332 ent = cfgBlock.entries;
333 while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
335 if(strcasecmp(ent->key, key) == 0)
336 break;
337 ent++;
340 if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
342 /* Allocate a new option entry */
343 ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
344 if(!ent)
346 ERR("config parse error: error reallocating config entries\n");
347 continue;
349 cfgBlock.entries = ent;
350 ent = cfgBlock.entries + cfgBlock.entryCount;
351 cfgBlock.entryCount++;
353 ent->key = strdup(key);
354 ent->value = NULL;
357 free(ent->value);
358 ent->value = expdup(value);
360 TRACE("found '%s' = '%s'\n", ent->key, ent->value);
363 free(buffer);
366 #ifdef _WIN32
367 void ReadALConfig(void)
369 al_string ppath = AL_STRING_INIT_STATIC();
370 WCHAR buffer[MAX_PATH];
371 const WCHAR *str;
372 FILE *f;
374 if(SHGetSpecialFolderPathW(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
376 al_string filepath = AL_STRING_INIT_STATIC();
377 alstr_copy_wcstr(&filepath, buffer);
378 alstr_append_cstr(&filepath, "\\alsoft.ini");
380 TRACE("Loading config %s...\n", alstr_get_cstr(filepath));
381 f = al_fopen(alstr_get_cstr(filepath), "rt");
382 if(f)
384 LoadConfigFromFile(f);
385 fclose(f);
387 alstr_reset(&filepath);
390 GetProcBinary(&ppath, NULL);
391 if(!alstr_empty(ppath))
393 alstr_append_cstr(&ppath, "\\alsoft.ini");
394 TRACE("Loading config %s...\n", alstr_get_cstr(ppath));
395 f = al_fopen(alstr_get_cstr(ppath), "r");
396 if(f)
398 LoadConfigFromFile(f);
399 fclose(f);
403 if((str=_wgetenv(L"ALSOFT_CONF")) != NULL && *str)
405 al_string filepath = AL_STRING_INIT_STATIC();
406 alstr_copy_wcstr(&filepath, str);
408 TRACE("Loading config %s...\n", alstr_get_cstr(filepath));
409 f = al_fopen(alstr_get_cstr(filepath), "rt");
410 if(f)
412 LoadConfigFromFile(f);
413 fclose(f);
415 alstr_reset(&filepath);
418 alstr_reset(&ppath);
420 #else
421 void ReadALConfig(void)
423 al_string confpaths = AL_STRING_INIT_STATIC();
424 al_string fname = AL_STRING_INIT_STATIC();
425 const char *str;
426 FILE *f;
428 str = "/etc/openal/alsoft.conf";
430 TRACE("Loading config %s...\n", str);
431 f = al_fopen(str, "r");
432 if(f)
434 LoadConfigFromFile(f);
435 fclose(f);
438 if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
439 str = "/etc/xdg";
440 alstr_copy_cstr(&confpaths, str);
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(!alstr_empty(confpaths))
448 char *next = strrchr(alstr_get_cstr(confpaths), ':');
449 if(next)
451 size_t len = next - alstr_get_cstr(confpaths);
452 alstr_copy_cstr(&fname, next+1);
453 VECTOR_RESIZE(confpaths, len, len+1);
454 VECTOR_ELEM(confpaths, len) = 0;
456 else
458 alstr_reset(&fname);
459 fname = confpaths;
460 AL_STRING_INIT(confpaths);
463 if(alstr_empty(fname) || VECTOR_FRONT(fname) != '/')
464 WARN("Ignoring XDG config dir: %s\n", alstr_get_cstr(fname));
465 else
467 if(VECTOR_BACK(fname) != '/') alstr_append_cstr(&fname, "/alsoft.conf");
468 else alstr_append_cstr(&fname, "alsoft.conf");
470 TRACE("Loading config %s...\n", alstr_get_cstr(fname));
471 f = al_fopen(alstr_get_cstr(fname), "r");
472 if(f)
474 LoadConfigFromFile(f);
475 fclose(f);
478 alstr_clear(&fname);
481 if((str=getenv("HOME")) != NULL && *str)
483 alstr_copy_cstr(&fname, str);
484 if(VECTOR_BACK(fname) != '/') alstr_append_cstr(&fname, "/.alsoftrc");
485 else alstr_append_cstr(&fname, ".alsoftrc");
487 TRACE("Loading config %s...\n", alstr_get_cstr(fname));
488 f = al_fopen(alstr_get_cstr(fname), "r");
489 if(f)
491 LoadConfigFromFile(f);
492 fclose(f);
496 if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
498 alstr_copy_cstr(&fname, str);
499 if(VECTOR_BACK(fname) != '/') alstr_append_cstr(&fname, "/alsoft.conf");
500 else alstr_append_cstr(&fname, "alsoft.conf");
502 else
504 alstr_clear(&fname);
505 if((str=getenv("HOME")) != NULL && str[0] != 0)
507 alstr_copy_cstr(&fname, str);
508 if(VECTOR_BACK(fname) != '/') alstr_append_cstr(&fname, "/.config/alsoft.conf");
509 else alstr_append_cstr(&fname, ".config/alsoft.conf");
512 if(!alstr_empty(fname))
514 TRACE("Loading config %s...\n", alstr_get_cstr(fname));
515 f = al_fopen(alstr_get_cstr(fname), "r");
516 if(f)
518 LoadConfigFromFile(f);
519 fclose(f);
523 alstr_clear(&fname);
524 GetProcBinary(&fname, NULL);
525 if(!alstr_empty(fname))
527 if(VECTOR_BACK(fname) != '/') alstr_append_cstr(&fname, "/alsoft.conf");
528 else alstr_append_cstr(&fname, "alsoft.conf");
530 TRACE("Loading config %s...\n", alstr_get_cstr(fname));
531 f = al_fopen(alstr_get_cstr(fname), "r");
532 if(f)
534 LoadConfigFromFile(f);
535 fclose(f);
539 if((str=getenv("ALSOFT_CONF")) != NULL && *str)
541 TRACE("Loading config %s...\n", str);
542 f = al_fopen(str, "r");
543 if(f)
545 LoadConfigFromFile(f);
546 fclose(f);
550 alstr_reset(&fname);
551 alstr_reset(&confpaths);
553 #endif
555 void FreeALConfig(void)
557 unsigned int i;
559 for(i = 0;i < cfgBlock.entryCount;i++)
561 free(cfgBlock.entries[i].key);
562 free(cfgBlock.entries[i].value);
564 free(cfgBlock.entries);
567 const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def)
569 unsigned int i;
570 char key[256];
572 if(!keyName)
573 return def;
575 if(blockName && strcasecmp(blockName, "general") != 0)
577 if(devName)
578 snprintf(key, sizeof(key), "%s/%s/%s", blockName, devName, keyName);
579 else
580 snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
582 else
584 if(devName)
585 snprintf(key, sizeof(key), "%s/%s", devName, keyName);
586 else
588 strncpy(key, keyName, sizeof(key)-1);
589 key[sizeof(key)-1] = 0;
593 for(i = 0;i < cfgBlock.entryCount;i++)
595 if(strcmp(cfgBlock.entries[i].key, key) == 0)
597 TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
598 if(cfgBlock.entries[i].value[0])
599 return cfgBlock.entries[i].value;
600 return def;
604 if(!devName)
606 TRACE("Key %s not found\n", key);
607 return def;
609 return GetConfigValue(NULL, blockName, keyName, def);
612 int ConfigValueExists(const char *devName, const char *blockName, const char *keyName)
614 const char *val = GetConfigValue(devName, blockName, keyName, "");
615 return !!val[0];
618 int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret)
620 const char *val = GetConfigValue(devName, blockName, keyName, "");
621 if(!val[0]) return 0;
623 *ret = val;
624 return 1;
627 int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret)
629 const char *val = GetConfigValue(devName, blockName, keyName, "");
630 if(!val[0]) return 0;
632 *ret = strtol(val, NULL, 0);
633 return 1;
636 int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret)
638 const char *val = GetConfigValue(devName, blockName, keyName, "");
639 if(!val[0]) return 0;
641 *ret = strtoul(val, NULL, 0);
642 return 1;
645 int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret)
647 const char *val = GetConfigValue(devName, blockName, keyName, "");
648 if(!val[0]) return 0;
650 #ifdef HAVE_STRTOF
651 *ret = strtof(val, NULL);
652 #else
653 *ret = (float)strtod(val, NULL);
654 #endif
655 return 1;
658 int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret)
660 const char *val = GetConfigValue(devName, blockName, keyName, "");
661 if(!val[0]) return 0;
663 *ret = (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
664 strcasecmp(val, "on") == 0 || atoi(val) != 0);
665 return 1;
668 int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def)
670 const char *val = GetConfigValue(devName, blockName, keyName, "");
672 if(!val[0]) return !!def;
673 return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
674 strcasecmp(val, "on") == 0 || atoi(val) != 0);