helpers.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2011 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 <time.h>
  30. #include <errno.h>
  31. #include <stdarg.h>
  32. #ifdef HAVE_MALLOC_H
  33. #include <malloc.h>
  34. #endif
  35. #ifdef HAVE_DIRENT_H
  36. #include <dirent.h>
  37. #endif
  38. #ifndef AL_NO_UID_DEFS
  39. #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
  40. #define INITGUID
  41. #include <windows.h>
  42. #ifdef HAVE_GUIDDEF_H
  43. #include <guiddef.h>
  44. #else
  45. #include <initguid.h>
  46. #endif
  47. DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
  48. DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
  49. DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
  50. DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
  51. DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
  52. DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
  53. DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
  54. DEFINE_GUID(IID_IAudioCaptureClient, 0xc8adbd64, 0xe71e, 0x48a0, 0xa4,0xde, 0x18,0x5c,0x39,0x5c,0xd3,0x17);
  55. #ifdef HAVE_MMDEVAPI
  56. #include <devpropdef.h>
  57. #include <propkeydef.h>
  58. DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
  59. DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_FormFactor, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 0);
  60. #endif
  61. #endif
  62. #endif /* AL_NO_UID_DEFS */
  63. #ifdef HAVE_DLFCN_H
  64. #include <dlfcn.h>
  65. #endif
  66. #ifdef HAVE_INTRIN_H
  67. #include <intrin.h>
  68. #endif
  69. #ifdef HAVE_CPUID_H
  70. #include <cpuid.h>
  71. #endif
  72. #ifdef HAVE_SYS_SYSCONF_H
  73. #include <sys/sysconf.h>
  74. #endif
  75. #ifdef HAVE_FLOAT_H
  76. #include <float.h>
  77. #endif
  78. #ifdef HAVE_IEEEFP_H
  79. #include <ieeefp.h>
  80. #endif
  81. #ifndef _WIN32
  82. #include <unistd.h>
  83. #elif defined(_WIN32_IE)
  84. #include <shlobj.h>
  85. #endif
  86. #include "alMain.h"
  87. #include "alu.h"
  88. #include "atomic.h"
  89. #include "uintmap.h"
  90. #include "vector.h"
  91. #include "alstring.h"
  92. #include "compat.h"
  93. #include "threads.h"
  94. extern inline ALuint NextPowerOf2(ALuint value);
  95. extern inline ALint fastf2i(ALfloat f);
  96. extern inline ALuint fastf2u(ALfloat f);
  97. ALuint CPUCapFlags = 0;
  98. void FillCPUCaps(ALuint capfilter)
  99. {
  100. ALuint caps = 0;
  101. /* FIXME: We really should get this for all available CPUs in case different
  102. * CPUs have different caps (is that possible on one machine?). */
  103. #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
  104. defined(_M_IX86) || defined(_M_X64))
  105. union {
  106. unsigned int regs[4];
  107. char str[sizeof(unsigned int[4])];
  108. } cpuinf[3];
  109. if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
  110. ERR("Failed to get CPUID\n");
  111. else
  112. {
  113. unsigned int maxfunc = cpuinf[0].regs[0];
  114. unsigned int maxextfunc = 0;
  115. if(__get_cpuid(0x80000000, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
  116. maxextfunc = cpuinf[0].regs[0];
  117. TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
  118. TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
  119. if(maxextfunc >= 0x80000004 &&
  120. __get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
  121. __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
  122. __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
  123. TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
  124. if(maxfunc >= 1 &&
  125. __get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
  126. {
  127. if((cpuinf[0].regs[3]&(1<<25)))
  128. {
  129. caps |= CPU_CAP_SSE;
  130. if((cpuinf[0].regs[3]&(1<<26)))
  131. {
  132. caps |= CPU_CAP_SSE2;
  133. if((cpuinf[0].regs[2]&(1<<0)))
  134. {
  135. caps |= CPU_CAP_SSE3;
  136. if((cpuinf[0].regs[2]&(1<<19)))
  137. caps |= CPU_CAP_SSE4_1;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
  144. defined(_M_IX86) || defined(_M_X64))
  145. union {
  146. int regs[4];
  147. char str[sizeof(int[4])];
  148. } cpuinf[3];
  149. (__cpuid)(cpuinf[0].regs, 0);
  150. if(cpuinf[0].regs[0] == 0)
  151. ERR("Failed to get CPUID\n");
  152. else
  153. {
  154. unsigned int maxfunc = cpuinf[0].regs[0];
  155. unsigned int maxextfunc;
  156. (__cpuid)(cpuinf[0].regs, 0x80000000);
  157. maxextfunc = cpuinf[0].regs[0];
  158. TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
  159. TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
  160. if(maxextfunc >= 0x80000004)
  161. {
  162. (__cpuid)(cpuinf[0].regs, 0x80000002);
  163. (__cpuid)(cpuinf[1].regs, 0x80000003);
  164. (__cpuid)(cpuinf[2].regs, 0x80000004);
  165. TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
  166. }
  167. if(maxfunc >= 1)
  168. {
  169. (__cpuid)(cpuinf[0].regs, 1);
  170. if((cpuinf[0].regs[3]&(1<<25)))
  171. {
  172. caps |= CPU_CAP_SSE;
  173. if((cpuinf[0].regs[3]&(1<<26)))
  174. {
  175. caps |= CPU_CAP_SSE2;
  176. if((cpuinf[0].regs[2]&(1<<0)))
  177. {
  178. caps |= CPU_CAP_SSE3;
  179. if((cpuinf[0].regs[2]&(1<<19)))
  180. caps |= CPU_CAP_SSE4_1;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. #else
  187. /* Assume support for whatever's supported if we can't check for it */
  188. #if defined(HAVE_SSE4_1)
  189. #warning "Assuming SSE 4.1 run-time support!"
  190. caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
  191. #elif defined(HAVE_SSE3)
  192. #warning "Assuming SSE 3 run-time support!"
  193. caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
  194. #elif defined(HAVE_SSE2)
  195. #warning "Assuming SSE 2 run-time support!"
  196. caps |= CPU_CAP_SSE | CPU_CAP_SSE2;
  197. #elif defined(HAVE_SSE)
  198. #warning "Assuming SSE run-time support!"
  199. caps |= CPU_CAP_SSE;
  200. #endif
  201. #endif
  202. #ifdef HAVE_NEON
  203. /* Assume Neon support if compiled with it */
  204. caps |= CPU_CAP_NEON;
  205. #endif
  206. TRACE("Extensions:%s%s%s%s%s%s\n",
  207. ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
  208. ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
  209. ((capfilter&CPU_CAP_SSE3) ? ((caps&CPU_CAP_SSE3) ? " +SSE3" : " -SSE3") : ""),
  210. ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
  211. ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +Neon" : " -Neon") : ""),
  212. ((!capfilter) ? " -none-" : "")
  213. );
  214. CPUCapFlags = caps & capfilter;
  215. }
  216. void *al_malloc(size_t alignment, size_t size)
  217. {
  218. #if defined(HAVE_ALIGNED_ALLOC)
  219. size = (size+(alignment-1))&~(alignment-1);
  220. return aligned_alloc(alignment, size);
  221. #elif defined(HAVE_POSIX_MEMALIGN)
  222. void *ret;
  223. if(posix_memalign(&ret, alignment, size) == 0)
  224. return ret;
  225. return NULL;
  226. #elif defined(HAVE__ALIGNED_MALLOC)
  227. return _aligned_malloc(size, alignment);
  228. #else
  229. char *ret = malloc(size+alignment);
  230. if(ret != NULL)
  231. {
  232. *(ret++) = 0x00;
  233. while(((ptrdiff_t)ret&(alignment-1)) != 0)
  234. *(ret++) = 0x55;
  235. }
  236. return ret;
  237. #endif
  238. }
  239. void *al_calloc(size_t alignment, size_t size)
  240. {
  241. void *ret = al_malloc(alignment, size);
  242. if(ret) memset(ret, 0, size);
  243. return ret;
  244. }
  245. void al_free(void *ptr)
  246. {
  247. #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
  248. free(ptr);
  249. #elif defined(HAVE__ALIGNED_MALLOC)
  250. _aligned_free(ptr);
  251. #else
  252. if(ptr != NULL)
  253. {
  254. char *finder = ptr;
  255. do {
  256. --finder;
  257. } while(*finder == 0x55);
  258. free(finder);
  259. }
  260. #endif
  261. }
  262. void SetMixerFPUMode(FPUCtl *ctl)
  263. {
  264. #ifdef HAVE_FENV_H
  265. fegetenv(STATIC_CAST(fenv_t, ctl));
  266. #if defined(__GNUC__) && defined(HAVE_SSE)
  267. /* FIXME: Some fegetenv implementations can get the SSE environment too?
  268. * How to tell when it does? */
  269. if((CPUCapFlags&CPU_CAP_SSE))
  270. __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
  271. #endif
  272. #ifdef FE_TOWARDZERO
  273. fesetround(FE_TOWARDZERO);
  274. #endif
  275. #if defined(__GNUC__) && defined(HAVE_SSE)
  276. if((CPUCapFlags&CPU_CAP_SSE))
  277. {
  278. int sseState = ctl->sse_state;
  279. sseState |= 0x6000; /* set round-to-zero */
  280. sseState |= 0x8000; /* set flush-to-zero */
  281. if((CPUCapFlags&CPU_CAP_SSE2))
  282. sseState |= 0x0040; /* set denormals-are-zero */
  283. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
  284. }
  285. #endif
  286. #elif defined(HAVE___CONTROL87_2)
  287. int mode;
  288. __control87_2(0, 0, &ctl->state, NULL);
  289. __control87_2(_RC_CHOP, _MCW_RC, &mode, NULL);
  290. #ifdef HAVE_SSE
  291. if((CPUCapFlags&CPU_CAP_SSE))
  292. {
  293. __control87_2(0, 0, NULL, &ctl->sse_state);
  294. __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
  295. }
  296. #endif
  297. #elif defined(HAVE__CONTROLFP)
  298. ctl->state = _controlfp(0, 0);
  299. (void)_controlfp(_RC_CHOP, _MCW_RC);
  300. #endif
  301. }
  302. void RestoreFPUMode(const FPUCtl *ctl)
  303. {
  304. #ifdef HAVE_FENV_H
  305. fesetenv(STATIC_CAST(fenv_t, ctl));
  306. #if defined(__GNUC__) && defined(HAVE_SSE)
  307. if((CPUCapFlags&CPU_CAP_SSE))
  308. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
  309. #endif
  310. #elif defined(HAVE___CONTROL87_2)
  311. int mode;
  312. __control87_2(ctl->state, _MCW_RC, &mode, NULL);
  313. #ifdef HAVE_SSE
  314. if((CPUCapFlags&CPU_CAP_SSE))
  315. __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
  316. #endif
  317. #elif defined(HAVE__CONTROLFP)
  318. _controlfp(ctl->state, _MCW_RC);
  319. #endif
  320. }
  321. static int StringSortCompare(const void *str1, const void *str2)
  322. {
  323. return al_string_cmp(*(const_al_string*)str1, *(const_al_string*)str2);
  324. }
  325. #ifdef _WIN32
  326. static WCHAR *FromUTF8(const char *str)
  327. {
  328. WCHAR *out = NULL;
  329. int len;
  330. if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
  331. {
  332. out = calloc(sizeof(WCHAR), len);
  333. MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
  334. }
  335. return out;
  336. }
  337. void *LoadLib(const char *name)
  338. {
  339. HANDLE hdl = NULL;
  340. WCHAR *wname;
  341. wname = FromUTF8(name);
  342. if(!wname)
  343. ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
  344. else
  345. {
  346. hdl = LoadLibraryW(wname);
  347. free(wname);
  348. }
  349. return hdl;
  350. }
  351. void CloseLib(void *handle)
  352. { FreeLibrary((HANDLE)handle); }
  353. void *GetSymbol(void *handle, const char *name)
  354. {
  355. void *ret;
  356. ret = (void*)GetProcAddress((HANDLE)handle, name);
  357. if(ret == NULL)
  358. ERR("Failed to load %s\n", name);
  359. return ret;
  360. }
  361. WCHAR *strdupW(const WCHAR *str)
  362. {
  363. const WCHAR *n;
  364. WCHAR *ret;
  365. size_t len;
  366. n = str;
  367. while(*n) n++;
  368. len = n - str;
  369. ret = calloc(sizeof(WCHAR), len+1);
  370. if(ret != NULL)
  371. memcpy(ret, str, sizeof(WCHAR)*len);
  372. return ret;
  373. }
  374. FILE *al_fopen(const char *fname, const char *mode)
  375. {
  376. WCHAR *wname=NULL, *wmode=NULL;
  377. FILE *file = NULL;
  378. wname = FromUTF8(fname);
  379. wmode = FromUTF8(mode);
  380. if(!wname)
  381. ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
  382. else if(!wmode)
  383. ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
  384. else
  385. file = _wfopen(wname, wmode);
  386. free(wname);
  387. free(wmode);
  388. return file;
  389. }
  390. void al_print(const char *type, const char *func, const char *fmt, ...)
  391. {
  392. char str[1024];
  393. WCHAR *wstr;
  394. va_list ap;
  395. va_start(ap, fmt);
  396. vsnprintf(str, sizeof(str), fmt, ap);
  397. va_end(ap);
  398. str[sizeof(str)-1] = 0;
  399. wstr = FromUTF8(str);
  400. if(!wstr)
  401. fprintf(LogFile, "AL lib: %s %s: <UTF-8 error> %s", type, func, str);
  402. else
  403. {
  404. fprintf(LogFile, "AL lib: %s %s: %ls", type, func, wstr);
  405. free(wstr);
  406. wstr = NULL;
  407. }
  408. fflush(LogFile);
  409. }
  410. static inline int is_slash(int c)
  411. { return (c == '\\' || c == '/'); }
  412. FILE *OpenDataFile(const char *fname, const char *subdir)
  413. {
  414. static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
  415. WCHAR *wname=NULL, *wsubdir=NULL;
  416. FILE *f;
  417. size_t i;
  418. wname = FromUTF8(fname);
  419. if(!wname)
  420. {
  421. ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
  422. return NULL;
  423. }
  424. /* If the path is absolute, open it directly. */
  425. if(wname[0] != '\0' && wname[1] == ':' && is_slash(wname[2]))
  426. {
  427. f = _wfopen(wname, L"rb");
  428. if(f) TRACE("Opened %s\n", fname);
  429. else WARN("Could not open %s\n", fname);
  430. free(wname);
  431. return f;
  432. }
  433. /* Try the current directory first before the data directories. */
  434. if((f=_wfopen(wname, L"rb")) != NULL)
  435. {
  436. TRACE("Opened %s\n", fname);
  437. free(wname);
  438. return f;
  439. }
  440. wsubdir = FromUTF8(subdir);
  441. if(!wsubdir)
  442. {
  443. ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir);
  444. free(wname);
  445. return NULL;
  446. }
  447. for(i = 0;i < COUNTOF(ids);i++)
  448. {
  449. WCHAR buffer[PATH_MAX];
  450. size_t len;
  451. if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) == FALSE)
  452. continue;
  453. len = lstrlenW(buffer);
  454. if(len > 0 && is_slash(buffer[len-1]))
  455. buffer[--len] = '\0';
  456. _snwprintf(buffer+len, PATH_MAX-len, L"/%ls/%ls", wsubdir, wname);
  457. len = lstrlenW(buffer);
  458. while(len > 0)
  459. {
  460. --len;
  461. if(buffer[len] == '/')
  462. buffer[len] = '\\';
  463. }
  464. if((f=_wfopen(buffer, L"rb")) != NULL)
  465. {
  466. al_string filepath = AL_STRING_INIT_STATIC();
  467. al_string_copy_wcstr(&filepath, buffer);
  468. TRACE("Opened %s\n", al_string_get_cstr(filepath));
  469. al_string_deinit(&filepath);
  470. break;
  471. }
  472. }
  473. free(wname);
  474. free(wsubdir);
  475. if(f == NULL)
  476. WARN("Could not open %s\\%s\n", subdir, fname);
  477. return f;
  478. }
  479. static size_t strlenW(const WCHAR *str)
  480. {
  481. const WCHAR *end = str;
  482. while(*end) ++end;
  483. return end-str;
  484. }
  485. static const WCHAR *strchrW(const WCHAR *str, WCHAR ch)
  486. {
  487. for(;*str != 0;++str)
  488. {
  489. if(*str == ch)
  490. return str;
  491. }
  492. return NULL;
  493. }
  494. static const WCHAR *strrchrW(const WCHAR *str, WCHAR ch)
  495. {
  496. const WCHAR *ret = NULL;
  497. for(;*str != 0;++str)
  498. {
  499. if(*str == ch)
  500. ret = str;
  501. }
  502. return ret;
  503. }
  504. static const WCHAR *strstrW(const WCHAR *haystack, const WCHAR *needle)
  505. {
  506. size_t len = strlenW(needle);
  507. while(*haystack != 0)
  508. {
  509. if(CompareStringW(GetThreadLocale(), NORM_IGNORECASE,
  510. haystack, len, needle, len) == CSTR_EQUAL)
  511. return haystack;
  512. do {
  513. ++haystack;
  514. } while(((*haystack)&0xC000) == 0x8000);
  515. }
  516. return NULL;
  517. }
  518. /* Compares the filename in the find data with the match string. The match
  519. * string may contain the "%r" marker to signifiy a sample rate (really any
  520. * positive integer), "%%" to signify a single '%', or "%s" for a (non-greedy)
  521. * string.
  522. */
  523. static int MatchFilter(const WCHAR *match, const WIN32_FIND_DATAW *fdata)
  524. {
  525. const WCHAR *name = fdata->cFileName;
  526. int ret = 1;
  527. do {
  528. const WCHAR *p = strchrW(match, '%');
  529. if(!p)
  530. ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE,
  531. match, -1, name, -1) == CSTR_EQUAL;
  532. else
  533. {
  534. int len = p-match;
  535. ret = lstrlenW(name) >= len;
  536. if(ret)
  537. ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE,
  538. match, len, name, len) == CSTR_EQUAL;
  539. if(ret)
  540. {
  541. match += len;
  542. name += len;
  543. ++p;
  544. if(*p == 'r')
  545. {
  546. unsigned long l = 0;
  547. while(*name >= '0' && *name <= '9')
  548. {
  549. l = l*10 + (*name-'0');
  550. ++name;
  551. }
  552. ret = l > 0;
  553. ++p;
  554. }
  555. else if(*p == 's')
  556. {
  557. const WCHAR *next = p+1;
  558. if(*next != '\0' && *next != '%')
  559. {
  560. const WCHAR *next_p = strchrW(next, '%');
  561. const WCHAR *m;
  562. if(!next_p)
  563. m = strstrW(name, next);
  564. else
  565. {
  566. WCHAR *tmp = malloc((next_p - next + 1) * 2);
  567. memcpy(tmp, next, (next_p - next) * 2);
  568. tmp[next_p - next] = 0;
  569. m = strstrW(name, tmp);
  570. free(tmp);
  571. }
  572. ret = !!m;
  573. if(ret)
  574. {
  575. size_t l;
  576. if(next_p) l = next_p - next;
  577. else l = strlenW(next);
  578. name = m + l;
  579. next += l;
  580. }
  581. }
  582. p = next;
  583. }
  584. }
  585. }
  586. match = p;
  587. } while(ret && match && *match);
  588. return ret;
  589. }
  590. static void RecurseDirectorySearch(const char *path, const WCHAR *match, vector_al_string *results)
  591. {
  592. WIN32_FIND_DATAW fdata;
  593. const WCHAR *sep, *p;
  594. HANDLE hdl;
  595. if(!match[0])
  596. return;
  597. /* Find the last directory separator and the next '%' marker in the match
  598. * string. */
  599. sep = strrchrW(match, '\\');
  600. p = strchrW(match, '%');
  601. /* If there's no separator, test the files in the specified path against
  602. * the match string, and add the results. */
  603. if(!sep)
  604. {
  605. al_string pathstr = AL_STRING_INIT_STATIC();
  606. WCHAR *wpath;
  607. TRACE("Searching %s for %ls\n", path, match);
  608. al_string_append_cstr(&pathstr, path);
  609. al_string_append_cstr(&pathstr, "\\*.*");
  610. wpath = FromUTF8(al_string_get_cstr(pathstr));
  611. hdl = FindFirstFileW(wpath, &fdata);
  612. if(hdl != INVALID_HANDLE_VALUE)
  613. {
  614. size_t base = VECTOR_SIZE(*results);
  615. do {
  616. if(MatchFilter(match, &fdata))
  617. {
  618. al_string str = AL_STRING_INIT_STATIC();
  619. al_string_copy_cstr(&str, path);
  620. al_string_append_char(&str, '\\');
  621. al_string_append_wcstr(&str, fdata.cFileName);
  622. TRACE("Got result %s\n", al_string_get_cstr(str));
  623. VECTOR_PUSH_BACK(*results, str);
  624. }
  625. } while(FindNextFileW(hdl, &fdata));
  626. FindClose(hdl);
  627. if(VECTOR_SIZE(*results) > base)
  628. qsort(VECTOR_ITER_BEGIN(*results)+base, VECTOR_SIZE(*results)-base,
  629. sizeof(VECTOR_FRONT(*results)), StringSortCompare);
  630. }
  631. free(wpath);
  632. al_string_deinit(&pathstr);
  633. return;
  634. }
  635. /* If there's no '%' marker, or it's after the final separator, append the
  636. * remaining directories to the path and recurse into it with the remaining
  637. * filename portion. */
  638. if(!p || p-sep >= 0)
  639. {
  640. al_string npath = AL_STRING_INIT_STATIC();
  641. al_string_append_cstr(&npath, path);
  642. al_string_append_char(&npath, '\\');
  643. al_string_append_wrange(&npath, match, sep);
  644. TRACE("Recursing into %s with %ls\n", al_string_get_cstr(npath), sep+1);
  645. RecurseDirectorySearch(al_string_get_cstr(npath), sep+1, results);
  646. al_string_deinit(&npath);
  647. return;
  648. }
  649. /* Look for the last separator before the '%' marker, and the first
  650. * separator after it. */
  651. sep = strchrW(match, '\\');
  652. if(sep-p >= 0) sep = NULL;
  653. for(;;)
  654. {
  655. const WCHAR *next = strchrW(sep?sep+1:match, '\\');
  656. if(next-p < 0)
  657. {
  658. al_string npath = AL_STRING_INIT_STATIC();
  659. WCHAR *nwpath, *nwmatch;
  660. /* Append up to the last directory before the one with a '%'. */
  661. al_string_copy_cstr(&npath, path);
  662. if(sep)
  663. {
  664. al_string_append_char(&npath, '\\');
  665. al_string_append_wrange(&npath, match, sep);
  666. }
  667. al_string_append_cstr(&npath, "\\*.*");
  668. nwpath = FromUTF8(al_string_get_cstr(npath));
  669. /* Take the directory name containing a '%' as a new string to
  670. * match against. */
  671. if(!sep)
  672. {
  673. nwmatch = calloc(2, next-match+1);
  674. memcpy(nwmatch, match, (next-match)*2);
  675. }
  676. else
  677. {
  678. nwmatch = calloc(2, next-(sep+1)+1);
  679. memcpy(nwmatch, sep+1, (next-(sep+1))*2);
  680. }
  681. /* For each matching directory name, recurse into it with the
  682. * remaining string. */
  683. TRACE("Searching %s for %ls\n", al_string_get_cstr(npath), nwmatch);
  684. hdl = FindFirstFileW(nwpath, &fdata);
  685. if(hdl != INVALID_HANDLE_VALUE)
  686. {
  687. do {
  688. if(MatchFilter(nwmatch, &fdata))
  689. {
  690. al_string ndir = AL_STRING_INIT_STATIC();
  691. al_string_copy(&ndir, npath);
  692. al_string_append_char(&ndir, '\\');
  693. al_string_append_wcstr(&ndir, fdata.cFileName);
  694. TRACE("Recursing %s with %ls\n", al_string_get_cstr(ndir), next+1);
  695. RecurseDirectorySearch(al_string_get_cstr(ndir), next+1, results);
  696. al_string_deinit(&ndir);
  697. }
  698. } while(FindNextFileW(hdl, &fdata));
  699. FindClose(hdl);
  700. }
  701. free(nwmatch);
  702. free(nwpath);
  703. al_string_deinit(&npath);
  704. break;
  705. }
  706. sep = next;
  707. }
  708. }
  709. vector_al_string SearchDataFiles(const char *match, const char *subdir)
  710. {
  711. static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
  712. static RefCount search_lock;
  713. vector_al_string results = VECTOR_INIT_STATIC();
  714. WCHAR *wmatch;
  715. size_t i;
  716. while(ATOMIC_EXCHANGE(uint, &search_lock, 1) == 1)
  717. althrd_yield();
  718. wmatch = FromUTF8(match);
  719. if(!wmatch)
  720. {
  721. ERR("Failed to convert UTF-8 filename: \"%s\"\n", match);
  722. return results;
  723. }
  724. for(i = 0;wmatch[i];++i)
  725. {
  726. if(wmatch[i] == '/')
  727. wmatch[i] = '\\';
  728. }
  729. /* If the path is absolute, use it directly. */
  730. if(isalpha(wmatch[0]) && wmatch[1] == ':' && is_slash(wmatch[2]))
  731. {
  732. char drv[3] = { (char)wmatch[0], ':', 0 };
  733. RecurseDirectorySearch(drv, wmatch+3, &results);
  734. }
  735. else if(wmatch[0] == '\\' && wmatch[1] == '\\' && wmatch[2] == '?' && wmatch[3] == '\\')
  736. RecurseDirectorySearch("\\\\?", wmatch+4, &results);
  737. else
  738. {
  739. al_string path = AL_STRING_INIT_STATIC();
  740. WCHAR *cwdbuf;
  741. /* Search the app-local directory. */
  742. if((cwdbuf=_wgetenv(L"ALSOFT_LOCAL_PATH")) && *cwdbuf != '\0')
  743. {
  744. al_string_copy_wcstr(&path, cwdbuf);
  745. if(is_slash(VECTOR_BACK(path)))
  746. {
  747. VECTOR_POP_BACK(path);
  748. *VECTOR_ITER_END(path) = 0;
  749. }
  750. }
  751. else if(!(cwdbuf=_wgetcwd(NULL, 0)))
  752. al_string_copy_cstr(&path, ".");
  753. else
  754. {
  755. al_string_copy_wcstr(&path, cwdbuf);
  756. if(is_slash(VECTOR_BACK(path)))
  757. {
  758. VECTOR_POP_BACK(path);
  759. *VECTOR_ITER_END(path) = 0;
  760. }
  761. free(cwdbuf);
  762. }
  763. #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
  764. VECTOR_FOR_EACH(char, path, FIX_SLASH);
  765. #undef FIX_SLASH
  766. RecurseDirectorySearch(al_string_get_cstr(path), wmatch, &results);
  767. /* Search the local and global data dirs. */
  768. for(i = 0;i < COUNTOF(ids);i++)
  769. {
  770. WCHAR buffer[PATH_MAX];
  771. if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) != FALSE)
  772. {
  773. al_string_copy_wcstr(&path, buffer);
  774. if(!is_slash(VECTOR_BACK(path)))
  775. al_string_append_char(&path, '\\');
  776. al_string_append_cstr(&path, subdir);
  777. #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
  778. VECTOR_FOR_EACH(char, path, FIX_SLASH);
  779. #undef FIX_SLASH
  780. RecurseDirectorySearch(al_string_get_cstr(path), wmatch, &results);
  781. }
  782. }
  783. al_string_deinit(&path);
  784. }
  785. free(wmatch);
  786. ATOMIC_STORE(&search_lock, 0);
  787. return results;
  788. }
  789. #else
  790. #ifdef HAVE_DLFCN_H
  791. void *LoadLib(const char *name)
  792. {
  793. const char *err;
  794. void *handle;
  795. dlerror();
  796. handle = dlopen(name, RTLD_NOW);
  797. if((err=dlerror()) != NULL)
  798. handle = NULL;
  799. return handle;
  800. }
  801. void CloseLib(void *handle)
  802. { dlclose(handle); }
  803. void *GetSymbol(void *handle, const char *name)
  804. {
  805. const char *err;
  806. void *sym;
  807. dlerror();
  808. sym = dlsym(handle, name);
  809. if((err=dlerror()) != NULL)
  810. {
  811. WARN("Failed to load %s: %s\n", name, err);
  812. sym = NULL;
  813. }
  814. return sym;
  815. }
  816. #endif /* HAVE_DLFCN_H */
  817. void al_print(const char *type, const char *func, const char *fmt, ...)
  818. {
  819. va_list ap;
  820. va_start(ap, fmt);
  821. fprintf(LogFile, "AL lib: %s %s: ", type, func);
  822. vfprintf(LogFile, fmt, ap);
  823. va_end(ap);
  824. fflush(LogFile);
  825. }
  826. FILE *OpenDataFile(const char *fname, const char *subdir)
  827. {
  828. char buffer[PATH_MAX] = "";
  829. const char *str, *next;
  830. FILE *f;
  831. if(fname[0] == '/')
  832. {
  833. if((f=al_fopen(fname, "rb")) != NULL)
  834. {
  835. TRACE("Opened %s\n", fname);
  836. return f;
  837. }
  838. WARN("Could not open %s\n", fname);
  839. return NULL;
  840. }
  841. if((f=al_fopen(fname, "rb")) != NULL)
  842. {
  843. TRACE("Opened %s\n", fname);
  844. return f;
  845. }
  846. if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
  847. snprintf(buffer, sizeof(buffer), "%s/%s/%s", str, subdir, fname);
  848. else if((str=getenv("HOME")) != NULL && str[0] != '\0')
  849. snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", str, subdir, fname);
  850. if(buffer[0])
  851. {
  852. if((f=al_fopen(buffer, "rb")) != NULL)
  853. {
  854. TRACE("Opened %s\n", buffer);
  855. return f;
  856. }
  857. }
  858. if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
  859. str = "/usr/local/share/:/usr/share/";
  860. next = str;
  861. while((str=next) != NULL && str[0] != '\0')
  862. {
  863. size_t len;
  864. next = strchr(str, ':');
  865. if(!next)
  866. len = strlen(str);
  867. else
  868. {
  869. len = next - str;
  870. next++;
  871. }
  872. if(len > sizeof(buffer)-1)
  873. len = sizeof(buffer)-1;
  874. strncpy(buffer, str, len);
  875. buffer[len] = '\0';
  876. snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname);
  877. if((f=al_fopen(buffer, "rb")) != NULL)
  878. {
  879. TRACE("Opened %s\n", buffer);
  880. return f;
  881. }
  882. }
  883. WARN("Could not open %s/%s\n", subdir, fname);
  884. return NULL;
  885. }
  886. static int MatchFilter(const char *name, const char *match)
  887. {
  888. int ret = 1;
  889. do {
  890. const char *p = strchr(match, '%');
  891. if(!p)
  892. ret = strcmp(match, name) == 0;
  893. else
  894. {
  895. size_t len = p-match;
  896. ret = strncmp(match, name, len) == 0;
  897. if(ret)
  898. {
  899. match += len;
  900. name += len;
  901. ++p;
  902. if(*p == 'r')
  903. {
  904. char *end;
  905. ret = strtoul(name, &end, 10) > 0;
  906. if(ret) name = end;
  907. ++p;
  908. }
  909. else if(*p == 's')
  910. {
  911. const char *next = p+1;
  912. if(*next != '\0' && *next != '%')
  913. {
  914. const char *next_p = strchr(next, '%');
  915. const char *m;
  916. if(!next_p)
  917. m = strstr(name, next);
  918. else
  919. {
  920. char *tmp = malloc(next_p - next + 1);
  921. memcpy(tmp, next, next_p - next);
  922. tmp[next_p - next] = 0;
  923. m = strstr(name, tmp);
  924. free(tmp);
  925. }
  926. ret = !!m;
  927. if(ret)
  928. {
  929. size_t l;
  930. if(next_p) l = next_p - next;
  931. else l = strlen(next);
  932. name = m + l;
  933. next += l;
  934. }
  935. }
  936. p = next;
  937. }
  938. }
  939. }
  940. match = p;
  941. } while(ret && match && *match);
  942. return ret;
  943. }
  944. static void RecurseDirectorySearch(const char *path, const char *match, vector_al_string *results)
  945. {
  946. char *sep, *p;
  947. if(!match[0])
  948. return;
  949. sep = strrchr(match, '/');
  950. p = strchr(match, '%');
  951. if(!sep)
  952. {
  953. DIR *dir;
  954. TRACE("Searching %s for %s\n", path?path:"/", match);
  955. dir = opendir(path?path:"/");
  956. if(dir != NULL)
  957. {
  958. size_t base = VECTOR_SIZE(*results);
  959. struct dirent *dirent;
  960. while((dirent=readdir(dir)) != NULL)
  961. {
  962. al_string str;
  963. if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0 ||
  964. !MatchFilter(dirent->d_name, match))
  965. continue;
  966. AL_STRING_INIT(str);
  967. if(path) al_string_copy_cstr(&str, path);
  968. al_string_append_char(&str, '/');
  969. al_string_append_cstr(&str, dirent->d_name);
  970. TRACE("Got result %s\n", al_string_get_cstr(str));
  971. VECTOR_PUSH_BACK(*results, str);
  972. }
  973. closedir(dir);
  974. if(VECTOR_SIZE(*results) > base)
  975. qsort(VECTOR_ITER_BEGIN(*results)+base, VECTOR_SIZE(*results)-base,
  976. sizeof(VECTOR_FRONT(*results)), StringSortCompare);
  977. }
  978. return;
  979. }
  980. if(!p || p-sep >= 0)
  981. {
  982. al_string npath = AL_STRING_INIT_STATIC();
  983. if(path) al_string_append_cstr(&npath, path);
  984. al_string_append_char(&npath, '/');
  985. al_string_append_range(&npath, match, sep);
  986. TRACE("Recursing into %s with %s\n", al_string_get_cstr(npath), sep+1);
  987. RecurseDirectorySearch(al_string_get_cstr(npath), sep+1, results);
  988. al_string_deinit(&npath);
  989. return;
  990. }
  991. sep = strchr(match, '/');
  992. if(sep-p >= 0) sep = NULL;
  993. for(;;)
  994. {
  995. char *next = strchr(sep?sep+1:match, '/');
  996. if(next-p < 0)
  997. {
  998. al_string npath = AL_STRING_INIT_STATIC();
  999. al_string nmatch = AL_STRING_INIT_STATIC();
  1000. const char *tomatch;
  1001. DIR *dir;
  1002. if(!sep)
  1003. {
  1004. al_string_append_cstr(&npath, path?path:"/.");
  1005. tomatch = match;
  1006. }
  1007. else
  1008. {
  1009. if(path) al_string_append_cstr(&npath, path);
  1010. al_string_append_char(&npath, '/');
  1011. al_string_append_range(&npath, match, sep);
  1012. al_string_append_range(&nmatch, sep+1, next);
  1013. tomatch = al_string_get_cstr(nmatch);
  1014. }
  1015. TRACE("Searching %s for %s\n", al_string_get_cstr(npath), tomatch);
  1016. dir = opendir(path?path:"/");
  1017. if(dir != NULL)
  1018. {
  1019. al_string ndir = AL_STRING_INIT_STATIC();
  1020. struct dirent *dirent;
  1021. while((dirent=readdir(dir)) != NULL)
  1022. {
  1023. if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0 ||
  1024. !MatchFilter(dirent->d_name, tomatch))
  1025. continue;
  1026. al_string_copy(&ndir, npath);
  1027. al_string_append_char(&ndir, '/');
  1028. al_string_append_cstr(&ndir, dirent->d_name);
  1029. TRACE("Recursing %s with %s\n", al_string_get_cstr(ndir), next+1);
  1030. RecurseDirectorySearch(al_string_get_cstr(ndir), next+1, results);
  1031. }
  1032. closedir(dir);
  1033. al_string_deinit(&ndir);
  1034. }
  1035. al_string_deinit(&nmatch);
  1036. al_string_deinit(&npath);
  1037. break;
  1038. }
  1039. sep = next;
  1040. }
  1041. }
  1042. vector_al_string SearchDataFiles(const char *match, const char *subdir)
  1043. {
  1044. static RefCount search_lock;
  1045. vector_al_string results = VECTOR_INIT_STATIC();
  1046. while(ATOMIC_EXCHANGE(uint, &search_lock, 1) == 1)
  1047. althrd_yield();
  1048. if(match[0] == '/')
  1049. RecurseDirectorySearch(NULL, match+1, &results);
  1050. else
  1051. {
  1052. al_string path = AL_STRING_INIT_STATIC();
  1053. const char *str, *next;
  1054. char cwdbuf[PATH_MAX];
  1055. /* Search the app-local directory. */
  1056. if((str=getenv("ALSOFT_LOCAL_PATH")) && *str != '\0')
  1057. {
  1058. strncpy(cwdbuf, str, sizeof(cwdbuf)-1);
  1059. cwdbuf[sizeof(cwdbuf)-1] = '\0';
  1060. }
  1061. else if(!getcwd(cwdbuf, sizeof(cwdbuf)))
  1062. strcpy(cwdbuf, ".");
  1063. RecurseDirectorySearch(cwdbuf, match, &results);
  1064. // Search local data dir
  1065. if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
  1066. {
  1067. al_string_append_cstr(&path, str);
  1068. al_string_append_char(&path, '/');
  1069. al_string_append_cstr(&path, subdir);
  1070. }
  1071. else if((str=getenv("HOME")) != NULL && str[0] != '\0')
  1072. {
  1073. al_string_append_cstr(&path, str);
  1074. al_string_append_cstr(&path, "/.local/share/");
  1075. al_string_append_cstr(&path, subdir);
  1076. }
  1077. if(!al_string_empty(path))
  1078. RecurseDirectorySearch(al_string_get_cstr(path), match, &results);
  1079. // Search global data dirs
  1080. if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
  1081. str = "/usr/local/share/:/usr/share/";
  1082. next = str;
  1083. while((str=next) != NULL && str[0] != '\0')
  1084. {
  1085. next = strchr(str, ':');
  1086. if(!next)
  1087. al_string_copy_cstr(&path, str);
  1088. else
  1089. {
  1090. al_string_clear(&path);
  1091. al_string_append_range(&path, str, next);
  1092. ++next;
  1093. }
  1094. if(!al_string_empty(path))
  1095. {
  1096. al_string_append_char(&path, '/');
  1097. al_string_append_cstr(&path, subdir);
  1098. RecurseDirectorySearch(al_string_get_cstr(path), match, &results);
  1099. }
  1100. }
  1101. al_string_deinit(&path);
  1102. }
  1103. ATOMIC_STORE(&search_lock, 0);
  1104. return results;
  1105. }
  1106. #endif
  1107. void SetRTPriority(void)
  1108. {
  1109. ALboolean failed = AL_FALSE;
  1110. #ifdef _WIN32
  1111. if(RTPrioLevel > 0)
  1112. failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  1113. #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
  1114. if(RTPrioLevel > 0)
  1115. {
  1116. struct sched_param param;
  1117. /* Use the minimum real-time priority possible for now (on Linux this
  1118. * should be 1 for SCHED_RR) */
  1119. param.sched_priority = sched_get_priority_min(SCHED_RR);
  1120. failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
  1121. }
  1122. #else
  1123. /* Real-time priority not available */
  1124. failed = (RTPrioLevel>0);
  1125. #endif
  1126. if(failed)
  1127. ERR("Failed to set priority level for thread\n");
  1128. }
  1129. ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, size_t obj_count, ALboolean exact)
  1130. {
  1131. vector_ *vecptr = (vector_*)ptr;
  1132. if((*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
  1133. {
  1134. size_t old_size = (*vecptr ? (*vecptr)->Size : 0);
  1135. void *temp;
  1136. /* Use the next power-of-2 size if we don't need to allocate the exact
  1137. * amount. This is preferred when regularly increasing the vector since
  1138. * it means fewer reallocations. Though it means it also wastes some
  1139. * memory. */
  1140. if(exact == AL_FALSE && obj_count < INT_MAX)
  1141. obj_count = NextPowerOf2((ALuint)obj_count);
  1142. /* Need to be explicit with the caller type's base size, because it
  1143. * could have extra padding before the start of the array (that is,
  1144. * sizeof(*vector_) may not equal base_size). */
  1145. temp = realloc(*vecptr, base_size + obj_size*obj_count);
  1146. if(temp == NULL) return AL_FALSE;
  1147. *vecptr = temp;
  1148. (*vecptr)->Capacity = obj_count;
  1149. (*vecptr)->Size = old_size;
  1150. }
  1151. return AL_TRUE;
  1152. }
  1153. ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, size_t obj_count)
  1154. {
  1155. vector_ *vecptr = (vector_*)ptr;
  1156. if(*vecptr || obj_count > 0)
  1157. {
  1158. if(!vector_reserve((char*)vecptr, base_size, obj_size, obj_count, AL_TRUE))
  1159. return AL_FALSE;
  1160. (*vecptr)->Size = obj_count;
  1161. }
  1162. return AL_TRUE;
  1163. }
  1164. ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend)
  1165. {
  1166. vector_ *vecptr = (vector_*)ptr;
  1167. if(datstart != datend)
  1168. {
  1169. ptrdiff_t ins_elem = (*vecptr ? ((char*)ins_pos - ((char*)(*vecptr) + base_size)) :
  1170. ((char*)ins_pos - (char*)NULL)) /
  1171. obj_size;
  1172. ptrdiff_t numins = ((const char*)datend - (const char*)datstart) / obj_size;
  1173. assert(numins > 0);
  1174. if((size_t)numins + VECTOR_SIZE(*vecptr) < (size_t)numins ||
  1175. !vector_reserve((char*)vecptr, base_size, obj_size, VECTOR_SIZE(*vecptr)+numins, AL_TRUE))
  1176. return AL_FALSE;
  1177. /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
  1178. if((size_t)ins_elem < (*vecptr)->Size)
  1179. {
  1180. memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
  1181. (char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
  1182. ((*vecptr)->Size-ins_elem)*obj_size);
  1183. }
  1184. memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
  1185. datstart, numins*obj_size);
  1186. (*vecptr)->Size += numins;
  1187. }
  1188. return AL_TRUE;
  1189. }
  1190. extern inline void al_string_deinit(al_string *str);
  1191. extern inline size_t al_string_length(const_al_string str);
  1192. extern inline ALboolean al_string_empty(const_al_string str);
  1193. extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
  1194. void al_string_clear(al_string *str)
  1195. {
  1196. /* Reserve one more character than the total size of the string. This is to
  1197. * ensure we have space to add a null terminator in the string data so it
  1198. * can be used as a C-style string. */
  1199. VECTOR_RESERVE(*str, 1);
  1200. VECTOR_RESIZE(*str, 0);
  1201. *VECTOR_ITER_END(*str) = 0;
  1202. }
  1203. static inline int al_string_compare(const al_string_char_type *str1, size_t str1len,
  1204. const al_string_char_type *str2, size_t str2len)
  1205. {
  1206. size_t complen = (str1len < str2len) ? str1len : str2len;
  1207. int ret = memcmp(str1, str2, complen);
  1208. if(ret == 0)
  1209. {
  1210. if(str1len > str2len) return 1;
  1211. if(str1len < str2len) return -1;
  1212. }
  1213. return ret;
  1214. }
  1215. int al_string_cmp(const_al_string str1, const_al_string str2)
  1216. {
  1217. return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
  1218. &VECTOR_FRONT(str2), al_string_length(str2));
  1219. }
  1220. int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
  1221. {
  1222. return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
  1223. str2, strlen(str2));
  1224. }
  1225. void al_string_copy(al_string *str, const_al_string from)
  1226. {
  1227. size_t len = al_string_length(from);
  1228. VECTOR_RESERVE(*str, len+1);
  1229. VECTOR_RESIZE(*str, 0);
  1230. VECTOR_INSERT(*str, VECTOR_ITER_END(*str), VECTOR_ITER_BEGIN(from), VECTOR_ITER_BEGIN(from)+len);
  1231. *VECTOR_ITER_END(*str) = 0;
  1232. }
  1233. void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
  1234. {
  1235. size_t len = strlen(from);
  1236. VECTOR_RESERVE(*str, len+1);
  1237. VECTOR_RESIZE(*str, 0);
  1238. VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
  1239. *VECTOR_ITER_END(*str) = 0;
  1240. }
  1241. void al_string_append_char(al_string *str, const al_string_char_type c)
  1242. {
  1243. VECTOR_RESERVE(*str, al_string_length(*str)+2);
  1244. VECTOR_PUSH_BACK(*str, c);
  1245. *VECTOR_ITER_END(*str) = 0;
  1246. }
  1247. void al_string_append_cstr(al_string *str, const al_string_char_type *from)
  1248. {
  1249. size_t len = strlen(from);
  1250. if(len != 0)
  1251. {
  1252. VECTOR_RESERVE(*str, al_string_length(*str)+len+1);
  1253. VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
  1254. *VECTOR_ITER_END(*str) = 0;
  1255. }
  1256. }
  1257. void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
  1258. {
  1259. if(to != from)
  1260. {
  1261. VECTOR_RESERVE(*str, al_string_length(*str)+(to-from)+1);
  1262. VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, to);
  1263. *VECTOR_ITER_END(*str) = 0;
  1264. }
  1265. }
  1266. #ifdef _WIN32
  1267. void al_string_copy_wcstr(al_string *str, const wchar_t *from)
  1268. {
  1269. int len;
  1270. if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
  1271. {
  1272. VECTOR_RESERVE(*str, len);
  1273. VECTOR_RESIZE(*str, len-1);
  1274. WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
  1275. *VECTOR_ITER_END(*str) = 0;
  1276. }
  1277. }
  1278. void al_string_append_wcstr(al_string *str, const wchar_t *from)
  1279. {
  1280. int len;
  1281. if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
  1282. {
  1283. size_t strlen = al_string_length(*str);
  1284. VECTOR_RESERVE(*str, strlen+len);
  1285. VECTOR_RESIZE(*str, strlen+len-1);
  1286. WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str) + strlen, len, NULL, NULL);
  1287. *VECTOR_ITER_END(*str) = 0;
  1288. }
  1289. }
  1290. void al_string_append_wrange(al_string *str, const wchar_t *from, const wchar_t *to)
  1291. {
  1292. int len;
  1293. if((len=WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), NULL, 0, NULL, NULL)) > 0)
  1294. {
  1295. size_t strlen = al_string_length(*str);
  1296. VECTOR_RESERVE(*str, strlen+len+1);
  1297. VECTOR_RESIZE(*str, strlen+len);
  1298. WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_FRONT(*str) + strlen, len+1, NULL, NULL);
  1299. *VECTOR_ITER_END(*str) = 0;
  1300. }
  1301. }
  1302. #endif