alcConfig.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #ifdef _WIN32
  21. #ifdef __MINGW32__
  22. #define _WIN32_IE 0x501
  23. #else
  24. #define _WIN32_IE 0x400
  25. #endif
  26. #endif
  27. #include "config.h"
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include <string.h>
  32. #ifdef _WIN32_IE
  33. #include <windows.h>
  34. #include <shlobj.h>
  35. #endif
  36. #include "alMain.h"
  37. #include "compat.h"
  38. #include "bool.h"
  39. typedef struct ConfigEntry {
  40. char *key;
  41. char *value;
  42. } ConfigEntry;
  43. typedef struct ConfigBlock {
  44. ConfigEntry *entries;
  45. unsigned int entryCount;
  46. } ConfigBlock;
  47. static ConfigBlock cfgBlock;
  48. static char *lstrip(char *line)
  49. {
  50. while(isspace(line[0]))
  51. line++;
  52. return line;
  53. }
  54. static char *rstrip(char *line)
  55. {
  56. size_t len = strlen(line);
  57. while(len > 0 && isspace(line[len-1]))
  58. len--;
  59. line[len] = 0;
  60. return line;
  61. }
  62. static int readline(FILE *f, char **output, size_t *maxlen)
  63. {
  64. size_t len = 0;
  65. int c;
  66. while((c=fgetc(f)) != EOF && (c == '\r' || c == '\n'))
  67. ;
  68. if(c == EOF)
  69. return 0;
  70. do {
  71. if(len+1 >= *maxlen)
  72. {
  73. void *temp = NULL;
  74. size_t newmax;
  75. newmax = (*maxlen ? (*maxlen)<<1 : 32);
  76. if(newmax > *maxlen)
  77. temp = realloc(*output, newmax);
  78. if(!temp)
  79. {
  80. ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, *maxlen);
  81. return 0;
  82. }
  83. *output = temp;
  84. *maxlen = newmax;
  85. }
  86. (*output)[len++] = c;
  87. (*output)[len] = '\0';
  88. } while((c=fgetc(f)) != EOF && c != '\r' && c != '\n');
  89. return 1;
  90. }
  91. static char *expdup(const char *str)
  92. {
  93. char *output = NULL;
  94. size_t maxlen = 0;
  95. size_t len = 0;
  96. while(*str != '\0')
  97. {
  98. const char *addstr;
  99. size_t addstrlen;
  100. size_t i;
  101. if(str[0] != '$')
  102. {
  103. const char *next = strchr(str, '$');
  104. addstr = str;
  105. addstrlen = next ? (size_t)(next-str) : strlen(str);
  106. str += addstrlen;
  107. }
  108. else
  109. {
  110. str++;
  111. if(*str == '$')
  112. {
  113. const char *next = strchr(str+1, '$');
  114. addstr = str;
  115. addstrlen = next ? (size_t)(next-str) : strlen(str);
  116. str += addstrlen;
  117. }
  118. else
  119. {
  120. bool hasbraces;
  121. char envname[1024];
  122. size_t k = 0;
  123. hasbraces = (*str == '{');
  124. if(hasbraces) str++;
  125. while((isalnum(*str) || *str == '_') && k < sizeof(envname)-1)
  126. envname[k++] = *(str++);
  127. envname[k++] = '\0';
  128. if(hasbraces && *str != '}')
  129. continue;
  130. if(hasbraces) str++;
  131. if((addstr=getenv(envname)) == NULL)
  132. continue;
  133. addstrlen = strlen(addstr);
  134. }
  135. }
  136. if(addstrlen == 0)
  137. continue;
  138. if(addstrlen >= maxlen-len)
  139. {
  140. void *temp = NULL;
  141. size_t newmax;
  142. newmax = len+addstrlen+1;
  143. if(newmax > maxlen)
  144. temp = realloc(output, newmax);
  145. if(!temp)
  146. {
  147. ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, maxlen);
  148. return output;
  149. }
  150. output = temp;
  151. maxlen = newmax;
  152. }
  153. for(i = 0;i < addstrlen;i++)
  154. output[len++] = addstr[i];
  155. output[len] = '\0';
  156. }
  157. return output ? output : calloc(1, 1);
  158. }
  159. static void LoadConfigFromFile(FILE *f)
  160. {
  161. char curSection[128] = "";
  162. char *buffer = NULL;
  163. size_t maxlen = 0;
  164. ConfigEntry *ent;
  165. while(readline(f, &buffer, &maxlen))
  166. {
  167. char *line, *comment;
  168. char key[256] = "";
  169. char value[256] = "";
  170. line = rstrip(lstrip(buffer));
  171. if(!line[0]) continue;
  172. if(line[0] == '[')
  173. {
  174. char *section = line+1;
  175. char *endsection;
  176. endsection = strchr(section, ']');
  177. if(!endsection || section == endsection)
  178. {
  179. ERR("config parse error: bad line \"%s\"\n", line);
  180. continue;
  181. }
  182. if(endsection[1] != 0)
  183. {
  184. char *end = endsection+1;
  185. while(isspace(*end))
  186. ++end;
  187. if(*end != 0 && *end != '#')
  188. {
  189. ERR("config parse error: bad line \"%s\"\n", line);
  190. continue;
  191. }
  192. }
  193. *endsection = 0;
  194. if(strcasecmp(section, "general") == 0)
  195. curSection[0] = 0;
  196. else
  197. {
  198. size_t len, p = 0;
  199. do {
  200. char *nextp = strchr(section, '%');
  201. if(!nextp)
  202. {
  203. strncpy(curSection+p, section, sizeof(curSection)-1-p);
  204. break;
  205. }
  206. len = nextp - section;
  207. if(len > sizeof(curSection)-1-p)
  208. len = sizeof(curSection)-1-p;
  209. strncpy(curSection+p, section, len);
  210. p += len;
  211. section = nextp;
  212. if(((section[1] >= '0' && section[1] <= '9') ||
  213. (section[1] >= 'a' && section[1] <= 'f') ||
  214. (section[1] >= 'A' && section[1] <= 'F')) &&
  215. ((section[2] >= '0' && section[2] <= '9') ||
  216. (section[2] >= 'a' && section[2] <= 'f') ||
  217. (section[2] >= 'A' && section[2] <= 'F')))
  218. {
  219. unsigned char b = 0;
  220. if(section[1] >= '0' && section[1] <= '9')
  221. b = (section[1]-'0') << 4;
  222. else if(section[1] >= 'a' && section[1] <= 'f')
  223. b = (section[1]-'a'+0xa) << 4;
  224. else if(section[1] >= 'A' && section[1] <= 'F')
  225. b = (section[1]-'A'+0x0a) << 4;
  226. if(section[2] >= '0' && section[2] <= '9')
  227. b |= (section[2]-'0');
  228. else if(section[2] >= 'a' && section[2] <= 'f')
  229. b |= (section[2]-'a'+0xa);
  230. else if(section[2] >= 'A' && section[2] <= 'F')
  231. b |= (section[2]-'A'+0x0a);
  232. if(p < sizeof(curSection)-1)
  233. curSection[p++] = b;
  234. section += 3;
  235. }
  236. else if(section[1] == '%')
  237. {
  238. if(p < sizeof(curSection)-1)
  239. curSection[p++] = '%';
  240. section += 2;
  241. }
  242. else
  243. {
  244. if(p < sizeof(curSection)-1)
  245. curSection[p++] = '%';
  246. section += 1;
  247. }
  248. if(p < sizeof(curSection)-1)
  249. curSection[p] = 0;
  250. } while(p < sizeof(curSection)-1 && *section != 0);
  251. curSection[sizeof(curSection)-1] = 0;
  252. }
  253. continue;
  254. }
  255. comment = strchr(line, '#');
  256. if(comment) *(comment++) = 0;
  257. if(!line[0]) continue;
  258. if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
  259. sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
  260. sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
  261. {
  262. /* sscanf doesn't handle '' or "" as empty values, so clip it
  263. * manually. */
  264. if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
  265. value[0] = 0;
  266. }
  267. else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
  268. {
  269. /* Special case for 'key =' */
  270. value[0] = 0;
  271. }
  272. else
  273. {
  274. ERR("config parse error: malformed option line: \"%s\"\n\n", line);
  275. continue;
  276. }
  277. rstrip(key);
  278. if(curSection[0] != 0)
  279. {
  280. size_t len = strlen(curSection);
  281. memmove(&key[len+1], key, sizeof(key)-1-len);
  282. key[len] = '/';
  283. memcpy(key, curSection, len);
  284. }
  285. /* Check if we already have this option set */
  286. ent = cfgBlock.entries;
  287. while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
  288. {
  289. if(strcasecmp(ent->key, key) == 0)
  290. break;
  291. ent++;
  292. }
  293. if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
  294. {
  295. /* Allocate a new option entry */
  296. ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
  297. if(!ent)
  298. {
  299. ERR("config parse error: error reallocating config entries\n");
  300. continue;
  301. }
  302. cfgBlock.entries = ent;
  303. ent = cfgBlock.entries + cfgBlock.entryCount;
  304. cfgBlock.entryCount++;
  305. ent->key = strdup(key);
  306. ent->value = NULL;
  307. }
  308. free(ent->value);
  309. ent->value = expdup(value);
  310. TRACE("found '%s' = '%s'\n", ent->key, ent->value);
  311. }
  312. free(buffer);
  313. }
  314. #ifdef _WIN32
  315. void ReadALConfig(void)
  316. {
  317. WCHAR buffer[PATH_MAX];
  318. const WCHAR *str;
  319. al_string ppath;
  320. FILE *f;
  321. if(SHGetSpecialFolderPathW(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
  322. {
  323. al_string filepath = AL_STRING_INIT_STATIC();
  324. alstr_copy_wcstr(&filepath, buffer);
  325. alstr_append_cstr(&filepath, "\\alsoft.ini");
  326. TRACE("Loading config %s...\n", alstr_get_cstr(filepath));
  327. f = al_fopen(alstr_get_cstr(filepath), "rt");
  328. if(f)
  329. {
  330. LoadConfigFromFile(f);
  331. fclose(f);
  332. }
  333. alstr_reset(&filepath);
  334. }
  335. ppath = GetProcPath();
  336. if(!alstr_empty(ppath))
  337. {
  338. alstr_append_cstr(&ppath, "\\alsoft.ini");
  339. TRACE("Loading config %s...\n", alstr_get_cstr(ppath));
  340. f = al_fopen(alstr_get_cstr(ppath), "r");
  341. if(f)
  342. {
  343. LoadConfigFromFile(f);
  344. fclose(f);
  345. }
  346. }
  347. if((str=_wgetenv(L"ALSOFT_CONF")) != NULL && *str)
  348. {
  349. al_string filepath = AL_STRING_INIT_STATIC();
  350. alstr_copy_wcstr(&filepath, str);
  351. TRACE("Loading config %s...\n", alstr_get_cstr(filepath));
  352. f = al_fopen(alstr_get_cstr(filepath), "rt");
  353. if(f)
  354. {
  355. LoadConfigFromFile(f);
  356. fclose(f);
  357. }
  358. alstr_reset(&filepath);
  359. }
  360. alstr_reset(&ppath);
  361. }
  362. #else
  363. void ReadALConfig(void)
  364. {
  365. char buffer[PATH_MAX];
  366. const char *str;
  367. al_string ppath;
  368. FILE *f;
  369. str = "/etc/openal/alsoft.conf";
  370. TRACE("Loading config %s...\n", str);
  371. f = al_fopen(str, "r");
  372. if(f)
  373. {
  374. LoadConfigFromFile(f);
  375. fclose(f);
  376. }
  377. if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
  378. str = "/etc/xdg";
  379. strncpy(buffer, str, sizeof(buffer)-1);
  380. buffer[sizeof(buffer)-1] = 0;
  381. /* Go through the list in reverse, since "the order of base directories
  382. * denotes their importance; the first directory listed is the most
  383. * important". Ergo, we need to load the settings from the later dirs
  384. * first so that the settings in the earlier dirs override them.
  385. */
  386. while(1)
  387. {
  388. char *next = strrchr(buffer, ':');
  389. if(next) *(next++) = 0;
  390. else next = buffer;
  391. if(next[0] != '/')
  392. WARN("Ignoring XDG config dir: %s\n", next);
  393. else
  394. {
  395. size_t len = strlen(next);
  396. strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len);
  397. buffer[sizeof(buffer)-1] = 0;
  398. TRACE("Loading config %s...\n", next);
  399. f = al_fopen(next, "r");
  400. if(f)
  401. {
  402. LoadConfigFromFile(f);
  403. fclose(f);
  404. }
  405. }
  406. if(next == buffer)
  407. break;
  408. }
  409. if((str=getenv("HOME")) != NULL && *str)
  410. {
  411. snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
  412. TRACE("Loading config %s...\n", buffer);
  413. f = al_fopen(buffer, "r");
  414. if(f)
  415. {
  416. LoadConfigFromFile(f);
  417. fclose(f);
  418. }
  419. }
  420. if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
  421. snprintf(buffer, sizeof(buffer), "%s/%s", str, "alsoft.conf");
  422. else
  423. {
  424. buffer[0] = 0;
  425. if((str=getenv("HOME")) != NULL && str[0] != 0)
  426. snprintf(buffer, sizeof(buffer), "%s/.config/%s", str, "alsoft.conf");
  427. }
  428. if(buffer[0] != 0)
  429. {
  430. TRACE("Loading config %s...\n", buffer);
  431. f = al_fopen(buffer, "r");
  432. if(f)
  433. {
  434. LoadConfigFromFile(f);
  435. fclose(f);
  436. }
  437. }
  438. ppath = GetProcPath();
  439. if(!alstr_empty(ppath))
  440. {
  441. alstr_append_cstr(&ppath, "/alsoft.conf");
  442. TRACE("Loading config %s...\n", alstr_get_cstr(ppath));
  443. f = al_fopen(alstr_get_cstr(ppath), "r");
  444. if(f)
  445. {
  446. LoadConfigFromFile(f);
  447. fclose(f);
  448. }
  449. }
  450. if((str=getenv("ALSOFT_CONF")) != NULL && *str)
  451. {
  452. TRACE("Loading config %s...\n", str);
  453. f = al_fopen(str, "r");
  454. if(f)
  455. {
  456. LoadConfigFromFile(f);
  457. fclose(f);
  458. }
  459. }
  460. alstr_reset(&ppath);
  461. }
  462. #endif
  463. void FreeALConfig(void)
  464. {
  465. unsigned int i;
  466. for(i = 0;i < cfgBlock.entryCount;i++)
  467. {
  468. free(cfgBlock.entries[i].key);
  469. free(cfgBlock.entries[i].value);
  470. }
  471. free(cfgBlock.entries);
  472. }
  473. const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def)
  474. {
  475. unsigned int i;
  476. char key[256];
  477. if(!keyName)
  478. return def;
  479. if(blockName && strcasecmp(blockName, "general") != 0)
  480. {
  481. if(devName)
  482. snprintf(key, sizeof(key), "%s/%s/%s", blockName, devName, keyName);
  483. else
  484. snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
  485. }
  486. else
  487. {
  488. if(devName)
  489. snprintf(key, sizeof(key), "%s/%s", devName, keyName);
  490. else
  491. {
  492. strncpy(key, keyName, sizeof(key)-1);
  493. key[sizeof(key)-1] = 0;
  494. }
  495. }
  496. for(i = 0;i < cfgBlock.entryCount;i++)
  497. {
  498. if(strcmp(cfgBlock.entries[i].key, key) == 0)
  499. {
  500. TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
  501. if(cfgBlock.entries[i].value[0])
  502. return cfgBlock.entries[i].value;
  503. return def;
  504. }
  505. }
  506. if(!devName)
  507. {
  508. TRACE("Key %s not found\n", key);
  509. return def;
  510. }
  511. return GetConfigValue(NULL, blockName, keyName, def);
  512. }
  513. int ConfigValueExists(const char *devName, const char *blockName, const char *keyName)
  514. {
  515. const char *val = GetConfigValue(devName, blockName, keyName, "");
  516. return !!val[0];
  517. }
  518. int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret)
  519. {
  520. const char *val = GetConfigValue(devName, blockName, keyName, "");
  521. if(!val[0]) return 0;
  522. *ret = val;
  523. return 1;
  524. }
  525. int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret)
  526. {
  527. const char *val = GetConfigValue(devName, blockName, keyName, "");
  528. if(!val[0]) return 0;
  529. *ret = strtol(val, NULL, 0);
  530. return 1;
  531. }
  532. int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret)
  533. {
  534. const char *val = GetConfigValue(devName, blockName, keyName, "");
  535. if(!val[0]) return 0;
  536. *ret = strtoul(val, NULL, 0);
  537. return 1;
  538. }
  539. int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret)
  540. {
  541. const char *val = GetConfigValue(devName, blockName, keyName, "");
  542. if(!val[0]) return 0;
  543. #ifdef HAVE_STRTOF
  544. *ret = strtof(val, NULL);
  545. #else
  546. *ret = (float)strtod(val, NULL);
  547. #endif
  548. return 1;
  549. }
  550. int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret)
  551. {
  552. const char *val = GetConfigValue(devName, blockName, keyName, "");
  553. 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;
  557. }
  558. int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def)
  559. {
  560. const char *val = GetConfigValue(devName, blockName, keyName, "");
  561. if(!val[0]) return !!def;
  562. return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
  563. strcasecmp(val, "on") == 0 || atoi(val) != 0);
  564. }