helpers.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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. #ifdef _WIN32
  322. static WCHAR *FromUTF8(const char *str)
  323. {
  324. WCHAR *out = NULL;
  325. int len;
  326. if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
  327. {
  328. out = calloc(sizeof(WCHAR), len);
  329. MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
  330. }
  331. return out;
  332. }
  333. void *LoadLib(const char *name)
  334. {
  335. HANDLE hdl = NULL;
  336. WCHAR *wname;
  337. wname = FromUTF8(name);
  338. if(!wname)
  339. ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
  340. else
  341. {
  342. hdl = LoadLibraryW(wname);
  343. free(wname);
  344. }
  345. return hdl;
  346. }
  347. void CloseLib(void *handle)
  348. { FreeLibrary((HANDLE)handle); }
  349. void *GetSymbol(void *handle, const char *name)
  350. {
  351. void *ret;
  352. ret = (void*)GetProcAddress((HANDLE)handle, name);
  353. if(ret == NULL)
  354. ERR("Failed to load %s\n", name);
  355. return ret;
  356. }
  357. WCHAR *strdupW(const WCHAR *str)
  358. {
  359. const WCHAR *n;
  360. WCHAR *ret;
  361. size_t len;
  362. n = str;
  363. while(*n) n++;
  364. len = n - str;
  365. ret = calloc(sizeof(WCHAR), len+1);
  366. if(ret != NULL)
  367. memcpy(ret, str, sizeof(WCHAR)*len);
  368. return ret;
  369. }
  370. FILE *al_fopen(const char *fname, const char *mode)
  371. {
  372. WCHAR *wname=NULL, *wmode=NULL;
  373. FILE *file = NULL;
  374. wname = FromUTF8(fname);
  375. wmode = FromUTF8(mode);
  376. if(!wname)
  377. ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
  378. else if(!wmode)
  379. ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
  380. else
  381. file = _wfopen(wname, wmode);
  382. free(wname);
  383. free(wmode);
  384. return file;
  385. }
  386. void al_print(const char *type, const char *func, const char *fmt, ...)
  387. {
  388. char str[1024];
  389. WCHAR *wstr;
  390. va_list ap;
  391. va_start(ap, fmt);
  392. vsnprintf(str, sizeof(str), fmt, ap);
  393. va_end(ap);
  394. str[sizeof(str)-1] = 0;
  395. wstr = FromUTF8(str);
  396. if(!wstr)
  397. fprintf(LogFile, "AL lib: %s %s: <UTF-8 error> %s", type, func, str);
  398. else
  399. {
  400. fprintf(LogFile, "AL lib: %s %s: %ls", type, func, wstr);
  401. free(wstr);
  402. wstr = NULL;
  403. }
  404. fflush(LogFile);
  405. }
  406. static inline int is_slash(int c)
  407. { return (c == '\\' || c == '/'); }
  408. FILE *OpenDataFile(const char *fname, const char *subdir)
  409. {
  410. static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
  411. WCHAR *wname=NULL, *wsubdir=NULL;
  412. FILE *f;
  413. size_t i;
  414. wname = FromUTF8(fname);
  415. if(!wname)
  416. {
  417. ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
  418. return NULL;
  419. }
  420. /* If the path is absolute, open it directly. */
  421. if(wname[0] != '\0' && wname[1] == ':' && is_slash(wname[2]))
  422. {
  423. f = _wfopen(wname, L"rb");
  424. if(f) TRACE("Opened %s\n", fname);
  425. else WARN("Could not open %s\n", fname);
  426. free(wname);
  427. return f;
  428. }
  429. /* Try the current directory first before the data directories. */
  430. if((f=_wfopen(wname, L"rb")) != NULL)
  431. {
  432. TRACE("Opened %s\n", fname);
  433. free(wname);
  434. return f;
  435. }
  436. wsubdir = FromUTF8(subdir);
  437. if(!wsubdir)
  438. {
  439. ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir);
  440. free(wname);
  441. return NULL;
  442. }
  443. for(i = 0;i < COUNTOF(ids);i++)
  444. {
  445. WCHAR buffer[PATH_MAX];
  446. size_t len;
  447. if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) == FALSE)
  448. continue;
  449. len = lstrlenW(buffer);
  450. if(len > 0 && is_slash(buffer[len-1]))
  451. buffer[--len] = '\0';
  452. _snwprintf(buffer+len, PATH_MAX-len, L"/%ls/%ls", wsubdir, wname);
  453. len = lstrlenW(buffer);
  454. while(len > 0)
  455. {
  456. --len;
  457. if(buffer[len] == '/')
  458. buffer[len] = '\\';
  459. }
  460. if((f=_wfopen(buffer, L"rb")) != NULL)
  461. {
  462. al_string filepath = AL_STRING_INIT_STATIC();
  463. al_string_copy_wcstr(&filepath, buffer);
  464. TRACE("Opened %s\n", al_string_get_cstr(filepath));
  465. al_string_deinit(&filepath);
  466. break;
  467. }
  468. }
  469. free(wname);
  470. free(wsubdir);
  471. if(f == NULL)
  472. WARN("Could not open %s\\%s\n", subdir, fname);
  473. return f;
  474. }
  475. static size_t strlenW(const WCHAR *str)
  476. {
  477. const WCHAR *end = str;
  478. while(*end) ++end;
  479. return end-str;
  480. }
  481. static const WCHAR *strchrW(const WCHAR *str, WCHAR ch)
  482. {
  483. for(;*str != 0;++str)
  484. {
  485. if(*str == ch)
  486. return str;
  487. }
  488. return NULL;
  489. }
  490. static const WCHAR *strrchrW(const WCHAR *str, WCHAR ch)
  491. {
  492. const WCHAR *ret = NULL;
  493. for(;*str != 0;++str)
  494. {
  495. if(*str == ch)
  496. ret = str;
  497. }
  498. return ret;
  499. }
  500. static const WCHAR *strstrW(const WCHAR *haystack, const WCHAR *needle)
  501. {
  502. size_t len = strlenW(needle);
  503. while(*haystack != 0)
  504. {
  505. if(CompareStringW(GetThreadLocale(), NORM_IGNORECASE,
  506. haystack, len, needle, len) == CSTR_EQUAL)
  507. return haystack;
  508. do {
  509. ++haystack;
  510. } while(((*haystack)&0xC000) == 0x8000);
  511. }
  512. return NULL;
  513. }
  514. /* Compares the filename in the find data with the match string. The match
  515. * string may contain the "%r" marker to signifiy a sample rate (really any
  516. * positive integer), "%%" to signify a single '%', or "%s" for a (non-greedy)
  517. * string.
  518. */
  519. static int MatchFilter(const WCHAR *match, const WIN32_FIND_DATAW *fdata)
  520. {
  521. const WCHAR *name = fdata->cFileName;
  522. int ret = 1;
  523. do {
  524. const WCHAR *p = strchrW(match, '%');
  525. if(!p)
  526. ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE,
  527. match, -1, name, -1) == CSTR_EQUAL;
  528. else
  529. {
  530. int len = p-match;
  531. ret = lstrlenW(name) >= len;
  532. if(ret)
  533. ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE,
  534. match, len, name, len) == CSTR_EQUAL;
  535. if(ret)
  536. {
  537. match += len;
  538. name += len;
  539. ++p;
  540. if(*p == 'r')
  541. {
  542. unsigned long l = 0;
  543. while(*name >= '0' && *name <= '9')
  544. {
  545. l = l*10 + (*name-'0');
  546. ++name;
  547. }
  548. ret = l > 0;
  549. ++p;
  550. }
  551. else if(*p == 's')
  552. {
  553. const WCHAR *next = p+1;
  554. if(*next != '\0' && *next != '%')
  555. {
  556. const WCHAR *next_p = strchrW(next, '%');
  557. const WCHAR *m;
  558. if(!next_p)
  559. m = strstrW(name, next);
  560. else
  561. {
  562. WCHAR *tmp = malloc((next_p - next + 1) * 2);
  563. memcpy(tmp, next, (next_p - next) * 2);
  564. tmp[next_p - next] = 0;
  565. m = strstrW(name, tmp);
  566. free(tmp);
  567. }
  568. ret = !!m;
  569. if(ret)
  570. {
  571. size_t l;
  572. if(next_p) l = next_p - next;
  573. else l = strlenW(next);
  574. name = m + l;
  575. next += l;
  576. }
  577. }
  578. p = next;
  579. }
  580. }
  581. }
  582. match = p;
  583. } while(ret && match && *match);
  584. return ret;
  585. }
  586. static void RecurseDirectorySearch(const char *path, const WCHAR *match, vector_al_string *results)
  587. {
  588. WIN32_FIND_DATAW fdata;
  589. const WCHAR *sep, *p;
  590. HANDLE hdl;
  591. if(!match[0])
  592. return;
  593. /* Find the last directory separator and the next '%' marker in the match
  594. * string. */
  595. sep = strrchrW(match, '\\');
  596. p = strchrW(match, '%');
  597. /* If there's no separator, test the files in the specified path against
  598. * the match string, and add the results. */
  599. if(!sep)
  600. {
  601. al_string pathstr = AL_STRING_INIT_STATIC();
  602. WCHAR *wpath;
  603. TRACE("Searching %s for %ls\n", path, match);
  604. al_string_append_cstr(&pathstr, path);
  605. al_string_append_cstr(&pathstr, "\\*.*");
  606. wpath = FromUTF8(al_string_get_cstr(pathstr));
  607. hdl = FindFirstFileW(wpath, &fdata);
  608. if(hdl != INVALID_HANDLE_VALUE)
  609. {
  610. do {
  611. if(MatchFilter(match, &fdata))
  612. {
  613. al_string str = AL_STRING_INIT_STATIC();
  614. al_string_copy_cstr(&str, path);
  615. al_string_append_char(&str, '\\');
  616. al_string_append_wcstr(&str, fdata.cFileName);
  617. TRACE("Got result %s\n", al_string_get_cstr(str));
  618. VECTOR_PUSH_BACK(*results, str);
  619. }
  620. } while(FindNextFileW(hdl, &fdata));
  621. FindClose(hdl);
  622. }
  623. free(wpath);
  624. al_string_deinit(&pathstr);
  625. return;
  626. }
  627. /* If there's no '%' marker, or it's after the final separator, append the
  628. * remaining directories to the path and recurse into it with the remaining
  629. * filename portion. */
  630. if(!p || p-sep >= 0)
  631. {
  632. al_string npath = AL_STRING_INIT_STATIC();
  633. al_string_append_cstr(&npath, path);
  634. al_string_append_char(&npath, '\\');
  635. al_string_append_wrange(&npath, match, sep);
  636. TRACE("Recursing into %s with %ls\n", al_string_get_cstr(npath), sep+1);
  637. RecurseDirectorySearch(al_string_get_cstr(npath), sep+1, results);
  638. al_string_deinit(&npath);
  639. return;
  640. }
  641. /* Look for the last separator before the '%' marker, and the first
  642. * separator after it. */
  643. sep = strchrW(match, '\\');
  644. if(sep-p >= 0) sep = NULL;
  645. for(;;)
  646. {
  647. const WCHAR *next = strchrW(sep?sep+1:match, '\\');
  648. if(next-p < 0)
  649. {
  650. al_string npath = AL_STRING_INIT_STATIC();
  651. WCHAR *nwpath, *nwmatch;
  652. /* Append up to the last directory before the one with a '%'. */
  653. al_string_copy_cstr(&npath, path);
  654. if(sep)
  655. {
  656. al_string_append_char(&npath, '\\');
  657. al_string_append_wrange(&npath, match, sep);
  658. }
  659. al_string_append_cstr(&npath, "\\*.*");
  660. nwpath = FromUTF8(al_string_get_cstr(npath));
  661. /* Take the directory name containing a '%' as a new string to
  662. * match against. */
  663. if(!sep)
  664. {
  665. nwmatch = calloc(2, next-match+1);
  666. memcpy(nwmatch, match, (next-match)*2);
  667. }
  668. else
  669. {
  670. nwmatch = calloc(2, next-(sep+1)+1);
  671. memcpy(nwmatch, sep+1, (next-(sep+1))*2);
  672. }
  673. /* For each matching directory name, recurse into it with the
  674. * remaining string. */
  675. TRACE("Searching %s for %ls\n", al_string_get_cstr(npath), nwmatch);
  676. hdl = FindFirstFileW(nwpath, &fdata);
  677. if(hdl != INVALID_HANDLE_VALUE)
  678. {
  679. do {
  680. if(MatchFilter(nwmatch, &fdata))
  681. {
  682. al_string ndir = AL_STRING_INIT_STATIC();
  683. al_string_copy(&ndir, npath);
  684. al_string_append_char(&ndir, '\\');
  685. al_string_append_wcstr(&ndir, fdata.cFileName);
  686. TRACE("Recursing %s with %ls\n", al_string_get_cstr(ndir), next+1);
  687. RecurseDirectorySearch(al_string_get_cstr(ndir), next+1, results);
  688. al_string_deinit(&ndir);
  689. }
  690. } while(FindNextFileW(hdl, &fdata));
  691. FindClose(hdl);
  692. }
  693. free(nwmatch);
  694. free(nwpath);
  695. al_string_deinit(&npath);
  696. break;
  697. }
  698. sep = next;
  699. }
  700. }
  701. vector_al_string SearchDataFiles(const char *match, const char *subdir)
  702. {
  703. static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
  704. static RefCount search_lock;
  705. vector_al_string results = VECTOR_INIT_STATIC();
  706. WCHAR *wmatch;
  707. size_t i;
  708. while(ATOMIC_EXCHANGE(uint, &search_lock, 1) == 1)
  709. althrd_yield();
  710. wmatch = FromUTF8(match);
  711. if(!wmatch)
  712. {
  713. ERR("Failed to convert UTF-8 filename: \"%s\"\n", match);
  714. return results;
  715. }
  716. for(i = 0;wmatch[i];++i)
  717. {
  718. if(wmatch[i] == '/')
  719. wmatch[i] = '\\';
  720. }
  721. /* If the path is absolute, use it directly. */
  722. if(isalpha(wmatch[0]) && wmatch[1] == ':' && is_slash(wmatch[2]))
  723. {
  724. char drv[3] = { (char)wmatch[0], ':', 0 };
  725. RecurseDirectorySearch(drv, wmatch+3, &results);
  726. }
  727. else if(wmatch[0] == '\\' && wmatch[1] == '\\' && wmatch[2] == '?' && wmatch[3] == '\\')
  728. RecurseDirectorySearch("\\\\?", wmatch+4, &results);
  729. else
  730. {
  731. al_string path = AL_STRING_INIT_STATIC();
  732. WCHAR *cwdbuf;
  733. /* Search the app-local directory. */
  734. if((cwdbuf=_wgetenv(L"ALSOFT_LOCAL_PATH")) && *cwdbuf != '\0')
  735. {
  736. al_string_copy_wcstr(&path, cwdbuf);
  737. if(is_slash(VECTOR_BACK(path)))
  738. {
  739. VECTOR_POP_BACK(path);
  740. *VECTOR_ITER_END(path) = 0;
  741. }
  742. }
  743. else if(!(cwdbuf=_wgetcwd(NULL, 0)))
  744. al_string_copy_cstr(&path, ".");
  745. else
  746. {
  747. al_string_copy_wcstr(&path, cwdbuf);
  748. if(is_slash(VECTOR_BACK(path)))
  749. {
  750. VECTOR_POP_BACK(path);
  751. *VECTOR_ITER_END(path) = 0;
  752. }
  753. free(cwdbuf);
  754. }
  755. #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
  756. VECTOR_FOR_EACH(char, path, FIX_SLASH);
  757. #undef FIX_SLASH
  758. RecurseDirectorySearch(al_string_get_cstr(path), wmatch, &results);
  759. /* Search the local and global data dirs. */
  760. for(i = 0;i < COUNTOF(ids);i++)
  761. {
  762. WCHAR buffer[PATH_MAX];
  763. if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) != FALSE)
  764. {
  765. al_string_copy_wcstr(&path, buffer);
  766. if(!is_slash(VECTOR_BACK(path)))
  767. al_string_append_char(&path, '\\');
  768. al_string_append_cstr(&path, subdir);
  769. #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
  770. VECTOR_FOR_EACH(char, path, FIX_SLASH);
  771. #undef FIX_SLASH
  772. RecurseDirectorySearch(al_string_get_cstr(path), wmatch, &results);
  773. }
  774. }
  775. al_string_deinit(&path);
  776. }
  777. free(wmatch);
  778. ATOMIC_STORE(&search_lock, 0);
  779. return results;
  780. }
  781. #else
  782. #ifdef HAVE_DLFCN_H
  783. void *LoadLib(const char *name)
  784. {
  785. const char *err;
  786. void *handle;
  787. dlerror();
  788. handle = dlopen(name, RTLD_NOW);
  789. if((err=dlerror()) != NULL)
  790. handle = NULL;
  791. return handle;
  792. }
  793. void CloseLib(void *handle)
  794. { dlclose(handle); }
  795. void *GetSymbol(void *handle, const char *name)
  796. {
  797. const char *err;
  798. void *sym;
  799. dlerror();
  800. sym = dlsym(handle, name);
  801. if((err=dlerror()) != NULL)
  802. {
  803. WARN("Failed to load %s: %s\n", name, err);
  804. sym = NULL;
  805. }
  806. return sym;
  807. }
  808. #endif /* HAVE_DLFCN_H */
  809. void al_print(const char *type, const char *func, const char *fmt, ...)
  810. {
  811. va_list ap;
  812. va_start(ap, fmt);
  813. fprintf(LogFile, "AL lib: %s %s: ", type, func);
  814. vfprintf(LogFile, fmt, ap);
  815. va_end(ap);
  816. fflush(LogFile);
  817. }
  818. FILE *OpenDataFile(const char *fname, const char *subdir)
  819. {
  820. char buffer[PATH_MAX] = "";
  821. const char *str, *next;
  822. FILE *f;
  823. if(fname[0] == '/')
  824. {
  825. if((f=al_fopen(fname, "rb")) != NULL)
  826. {
  827. TRACE("Opened %s\n", fname);
  828. return f;
  829. }
  830. WARN("Could not open %s\n", fname);
  831. return NULL;
  832. }
  833. if((f=al_fopen(fname, "rb")) != NULL)
  834. {
  835. TRACE("Opened %s\n", fname);
  836. return f;
  837. }
  838. if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
  839. snprintf(buffer, sizeof(buffer), "%s/%s/%s", str, subdir, fname);
  840. else if((str=getenv("HOME")) != NULL && str[0] != '\0')
  841. snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", str, subdir, fname);
  842. if(buffer[0])
  843. {
  844. if((f=al_fopen(buffer, "rb")) != NULL)
  845. {
  846. TRACE("Opened %s\n", buffer);
  847. return f;
  848. }
  849. }
  850. if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
  851. str = "/usr/local/share/:/usr/share/";
  852. next = str;
  853. while((str=next) != NULL && str[0] != '\0')
  854. {
  855. size_t len;
  856. next = strchr(str, ':');
  857. if(!next)
  858. len = strlen(str);
  859. else
  860. {
  861. len = next - str;
  862. next++;
  863. }
  864. if(len > sizeof(buffer)-1)
  865. len = sizeof(buffer)-1;
  866. strncpy(buffer, str, len);
  867. buffer[len] = '\0';
  868. snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname);
  869. if((f=al_fopen(buffer, "rb")) != NULL)
  870. {
  871. TRACE("Opened %s\n", buffer);
  872. return f;
  873. }
  874. }
  875. WARN("Could not open %s/%s\n", subdir, fname);
  876. return NULL;
  877. }
  878. static const char *MatchString;
  879. static int MatchFilter(const struct dirent *dir)
  880. {
  881. const char *match = MatchString;
  882. const char *name = dir->d_name;
  883. int ret = 1;
  884. do {
  885. const char *p = strchr(match, '%');
  886. if(!p)
  887. ret = strcmp(match, name) == 0;
  888. else
  889. {
  890. size_t len = p-match;
  891. ret = strncmp(match, name, len) == 0;
  892. if(ret)
  893. {
  894. match += len;
  895. name += len;
  896. ++p;
  897. if(*p == 'r')
  898. {
  899. char *end;
  900. ret = strtoul(name, &end, 10) > 0;
  901. if(ret) name = end;
  902. ++p;
  903. }
  904. else if(*p == 's')
  905. {
  906. const char *next = p+1;
  907. if(*next != '\0' && *next != '%')
  908. {
  909. const char *next_p = strchr(next, '%');
  910. const char *m;
  911. if(!next_p)
  912. m = strstr(name, next);
  913. else
  914. {
  915. char *tmp = malloc(next_p - next + 1);
  916. memcpy(tmp, next, next_p - next);
  917. tmp[next_p - next] = 0;
  918. m = strstr(name, tmp);
  919. free(tmp);
  920. }
  921. ret = !!m;
  922. if(ret)
  923. {
  924. size_t l;
  925. if(next_p) l = next_p - next;
  926. else l = strlen(next);
  927. name = m + l;
  928. next += l;
  929. }
  930. }
  931. p = next;
  932. }
  933. }
  934. }
  935. match = p;
  936. } while(ret && match && *match);
  937. return ret;
  938. }
  939. static void RecurseDirectorySearch(const char *path, const char *match, vector_al_string *results)
  940. {
  941. struct dirent **namelist;
  942. char *sep, *p;
  943. int n, i;
  944. if(!match[0])
  945. return;
  946. sep = strrchr(match, '/');
  947. p = strchr(match, '%');
  948. if(!sep)
  949. {
  950. MatchString = match;
  951. TRACE("Searching %s for %s\n", path?path:"/", match);
  952. n = scandir(path?path:"/", &namelist, MatchFilter, alphasort);
  953. if(n >= 0)
  954. {
  955. for(i = 0;i < n;++i)
  956. {
  957. al_string str = AL_STRING_INIT_STATIC();
  958. if(path) al_string_copy_cstr(&str, path);
  959. al_string_append_char(&str, '/');
  960. al_string_append_cstr(&str, namelist[i]->d_name);
  961. TRACE("Got result %s\n", al_string_get_cstr(str));
  962. VECTOR_PUSH_BACK(*results, str);
  963. free(namelist[i]);
  964. }
  965. free(namelist);
  966. }
  967. return;
  968. }
  969. if(!p || p-sep >= 0)
  970. {
  971. al_string npath = AL_STRING_INIT_STATIC();
  972. if(path) al_string_append_cstr(&npath, path);
  973. al_string_append_char(&npath, '/');
  974. al_string_append_range(&npath, match, sep);
  975. TRACE("Recursing into %s with %s\n", al_string_get_cstr(npath), sep+1);
  976. RecurseDirectorySearch(al_string_get_cstr(npath), sep+1, results);
  977. al_string_deinit(&npath);
  978. return;
  979. }
  980. sep = strchr(match, '/');
  981. if(sep-p >= 0) sep = NULL;
  982. for(;;)
  983. {
  984. char *next = strchr(sep?sep+1:match, '/');
  985. if(next-p < 0)
  986. {
  987. al_string npath = AL_STRING_INIT_STATIC();
  988. al_string nmatch = AL_STRING_INIT_STATIC();
  989. if(!sep)
  990. {
  991. al_string_append_cstr(&npath, path?path:"/.");
  992. MatchString = match;
  993. }
  994. else
  995. {
  996. if(path) al_string_append_cstr(&npath, path);
  997. al_string_append_char(&npath, '/');
  998. al_string_append_range(&npath, match, sep);
  999. al_string_append_range(&nmatch, sep+1, next);
  1000. MatchString = al_string_get_cstr(nmatch);
  1001. }
  1002. TRACE("Searching %s for %s\n", al_string_get_cstr(npath), MatchString);
  1003. n = scandir(al_string_get_cstr(npath), &namelist, MatchFilter, alphasort);
  1004. if(n >= 0)
  1005. {
  1006. al_string ndir = AL_STRING_INIT_STATIC();
  1007. for(i = 0;i < n;++i)
  1008. {
  1009. al_string_copy(&ndir, npath);
  1010. al_string_append_char(&ndir, '/');
  1011. al_string_append_cstr(&ndir, namelist[i]->d_name);
  1012. free(namelist[i]);
  1013. TRACE("Recursing %s with %s\n", al_string_get_cstr(ndir), next+1);
  1014. RecurseDirectorySearch(al_string_get_cstr(ndir), next+1, results);
  1015. }
  1016. al_string_deinit(&ndir);
  1017. free(namelist);
  1018. }
  1019. al_string_deinit(&nmatch);
  1020. al_string_deinit(&npath);
  1021. break;
  1022. }
  1023. sep = next;
  1024. }
  1025. }
  1026. vector_al_string SearchDataFiles(const char *match, const char *subdir)
  1027. {
  1028. static RefCount search_lock;
  1029. vector_al_string results = VECTOR_INIT_STATIC();
  1030. while(ATOMIC_EXCHANGE(uint, &search_lock, 1) == 1)
  1031. althrd_yield();
  1032. if(match[0] == '/')
  1033. RecurseDirectorySearch(NULL, match+1, &results);
  1034. else
  1035. {
  1036. al_string path = AL_STRING_INIT_STATIC();
  1037. const char *str, *next;
  1038. char cwdbuf[PATH_MAX];
  1039. /* Search the app-local directory. */
  1040. if((str=getenv("ALSOFT_LOCAL_PATH")) && *str != '\0')
  1041. {
  1042. strncpy(cwdbuf, str, sizeof(cwdbuf)-1);
  1043. cwdbuf[sizeof(cwdbuf)-1] = '\0';
  1044. }
  1045. else if(!getcwd(cwdbuf, sizeof(cwdbuf)))
  1046. strcpy(cwdbuf, ".");
  1047. RecurseDirectorySearch(cwdbuf, match, &results);
  1048. // Search local data dir
  1049. if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
  1050. {
  1051. al_string_append_cstr(&path, str);
  1052. al_string_append_char(&path, '/');
  1053. al_string_append_cstr(&path, subdir);
  1054. }
  1055. else if((str=getenv("HOME")) != NULL && str[0] != '\0')
  1056. {
  1057. al_string_append_cstr(&path, str);
  1058. al_string_append_cstr(&path, "/.local/share/");
  1059. al_string_append_cstr(&path, subdir);
  1060. }
  1061. if(!al_string_empty(path))
  1062. RecurseDirectorySearch(al_string_get_cstr(path), match, &results);
  1063. // Search global data dirs
  1064. if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
  1065. str = "/usr/local/share/:/usr/share/";
  1066. next = str;
  1067. while((str=next) != NULL && str[0] != '\0')
  1068. {
  1069. next = strchr(str, ':');
  1070. if(!next)
  1071. al_string_copy_cstr(&path, str);
  1072. else
  1073. {
  1074. al_string_clear(&path);
  1075. al_string_append_range(&path, str, next);
  1076. ++next;
  1077. }
  1078. if(!al_string_empty(path))
  1079. {
  1080. al_string_append_char(&path, '/');
  1081. al_string_append_cstr(&path, subdir);
  1082. RecurseDirectorySearch(al_string_get_cstr(path), match, &results);
  1083. }
  1084. }
  1085. al_string_deinit(&path);
  1086. }
  1087. ATOMIC_STORE(&search_lock, 0);
  1088. return results;
  1089. }
  1090. #endif
  1091. void SetRTPriority(void)
  1092. {
  1093. ALboolean failed = AL_FALSE;
  1094. #ifdef _WIN32
  1095. if(RTPrioLevel > 0)
  1096. failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  1097. #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
  1098. if(RTPrioLevel > 0)
  1099. {
  1100. struct sched_param param;
  1101. /* Use the minimum real-time priority possible for now (on Linux this
  1102. * should be 1 for SCHED_RR) */
  1103. param.sched_priority = sched_get_priority_min(SCHED_RR);
  1104. failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
  1105. }
  1106. #else
  1107. /* Real-time priority not available */
  1108. failed = (RTPrioLevel>0);
  1109. #endif
  1110. if(failed)
  1111. ERR("Failed to set priority level for thread\n");
  1112. }
  1113. ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, size_t obj_count, ALboolean exact)
  1114. {
  1115. vector_ *vecptr = (vector_*)ptr;
  1116. if((*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
  1117. {
  1118. size_t old_size = (*vecptr ? (*vecptr)->Size : 0);
  1119. void *temp;
  1120. /* Use the next power-of-2 size if we don't need to allocate the exact
  1121. * amount. This is preferred when regularly increasing the vector since
  1122. * it means fewer reallocations. Though it means it also wastes some
  1123. * memory. */
  1124. if(exact == AL_FALSE && obj_count < INT_MAX)
  1125. obj_count = NextPowerOf2((ALuint)obj_count);
  1126. /* Need to be explicit with the caller type's base size, because it
  1127. * could have extra padding before the start of the array (that is,
  1128. * sizeof(*vector_) may not equal base_size). */
  1129. temp = realloc(*vecptr, base_size + obj_size*obj_count);
  1130. if(temp == NULL) return AL_FALSE;
  1131. *vecptr = temp;
  1132. (*vecptr)->Capacity = obj_count;
  1133. (*vecptr)->Size = old_size;
  1134. }
  1135. return AL_TRUE;
  1136. }
  1137. ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, size_t obj_count)
  1138. {
  1139. vector_ *vecptr = (vector_*)ptr;
  1140. if(*vecptr || obj_count > 0)
  1141. {
  1142. if(!vector_reserve((char*)vecptr, base_size, obj_size, obj_count, AL_TRUE))
  1143. return AL_FALSE;
  1144. (*vecptr)->Size = obj_count;
  1145. }
  1146. return AL_TRUE;
  1147. }
  1148. ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend)
  1149. {
  1150. vector_ *vecptr = (vector_*)ptr;
  1151. if(datstart != datend)
  1152. {
  1153. ptrdiff_t ins_elem = (*vecptr ? ((char*)ins_pos - ((char*)(*vecptr) + base_size)) :
  1154. ((char*)ins_pos - (char*)NULL)) /
  1155. obj_size;
  1156. ptrdiff_t numins = ((const char*)datend - (const char*)datstart) / obj_size;
  1157. assert(numins > 0);
  1158. if((size_t)numins + VECTOR_SIZE(*vecptr) < (size_t)numins ||
  1159. !vector_reserve((char*)vecptr, base_size, obj_size, VECTOR_SIZE(*vecptr)+numins, AL_TRUE))
  1160. return AL_FALSE;
  1161. /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
  1162. if((size_t)ins_elem < (*vecptr)->Size)
  1163. {
  1164. memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
  1165. (char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
  1166. ((*vecptr)->Size-ins_elem)*obj_size);
  1167. }
  1168. memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
  1169. datstart, numins*obj_size);
  1170. (*vecptr)->Size += numins;
  1171. }
  1172. return AL_TRUE;
  1173. }
  1174. extern inline void al_string_deinit(al_string *str);
  1175. extern inline size_t al_string_length(const_al_string str);
  1176. extern inline ALboolean al_string_empty(const_al_string str);
  1177. extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
  1178. void al_string_clear(al_string *str)
  1179. {
  1180. /* Reserve one more character than the total size of the string. This is to
  1181. * ensure we have space to add a null terminator in the string data so it
  1182. * can be used as a C-style string. */
  1183. VECTOR_RESERVE(*str, 1);
  1184. VECTOR_RESIZE(*str, 0);
  1185. *VECTOR_ITER_END(*str) = 0;
  1186. }
  1187. static inline int al_string_compare(const al_string_char_type *str1, size_t str1len,
  1188. const al_string_char_type *str2, size_t str2len)
  1189. {
  1190. size_t complen = (str1len < str2len) ? str1len : str2len;
  1191. int ret = memcmp(str1, str2, complen);
  1192. if(ret == 0)
  1193. {
  1194. if(str1len > str2len) return 1;
  1195. if(str1len < str2len) return -1;
  1196. }
  1197. return ret;
  1198. }
  1199. int al_string_cmp(const_al_string str1, const_al_string str2)
  1200. {
  1201. return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
  1202. &VECTOR_FRONT(str2), al_string_length(str2));
  1203. }
  1204. int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
  1205. {
  1206. return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
  1207. str2, strlen(str2));
  1208. }
  1209. void al_string_copy(al_string *str, const_al_string from)
  1210. {
  1211. size_t len = al_string_length(from);
  1212. VECTOR_RESERVE(*str, len+1);
  1213. VECTOR_RESIZE(*str, 0);
  1214. VECTOR_INSERT(*str, VECTOR_ITER_END(*str), VECTOR_ITER_BEGIN(from), VECTOR_ITER_BEGIN(from)+len);
  1215. *VECTOR_ITER_END(*str) = 0;
  1216. }
  1217. void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
  1218. {
  1219. size_t len = strlen(from);
  1220. VECTOR_RESERVE(*str, len+1);
  1221. VECTOR_RESIZE(*str, 0);
  1222. VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
  1223. *VECTOR_ITER_END(*str) = 0;
  1224. }
  1225. void al_string_append_char(al_string *str, const al_string_char_type c)
  1226. {
  1227. VECTOR_RESERVE(*str, al_string_length(*str)+2);
  1228. VECTOR_PUSH_BACK(*str, c);
  1229. *VECTOR_ITER_END(*str) = 0;
  1230. }
  1231. void al_string_append_cstr(al_string *str, const al_string_char_type *from)
  1232. {
  1233. size_t len = strlen(from);
  1234. if(len != 0)
  1235. {
  1236. VECTOR_RESERVE(*str, al_string_length(*str)+len+1);
  1237. VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
  1238. *VECTOR_ITER_END(*str) = 0;
  1239. }
  1240. }
  1241. void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
  1242. {
  1243. if(to != from)
  1244. {
  1245. VECTOR_RESERVE(*str, al_string_length(*str)+(to-from)+1);
  1246. VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, to);
  1247. *VECTOR_ITER_END(*str) = 0;
  1248. }
  1249. }
  1250. #ifdef _WIN32
  1251. void al_string_copy_wcstr(al_string *str, const wchar_t *from)
  1252. {
  1253. int len;
  1254. if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
  1255. {
  1256. VECTOR_RESERVE(*str, len);
  1257. VECTOR_RESIZE(*str, len-1);
  1258. WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
  1259. *VECTOR_ITER_END(*str) = 0;
  1260. }
  1261. }
  1262. void al_string_append_wcstr(al_string *str, const wchar_t *from)
  1263. {
  1264. int len;
  1265. if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
  1266. {
  1267. size_t strlen = al_string_length(*str);
  1268. VECTOR_RESERVE(*str, strlen+len);
  1269. VECTOR_RESIZE(*str, strlen+len-1);
  1270. WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str) + strlen, len, NULL, NULL);
  1271. *VECTOR_ITER_END(*str) = 0;
  1272. }
  1273. }
  1274. void al_string_append_wrange(al_string *str, const wchar_t *from, const wchar_t *to)
  1275. {
  1276. int len;
  1277. if((len=WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), NULL, 0, NULL, NULL)) > 0)
  1278. {
  1279. size_t strlen = al_string_length(*str);
  1280. VECTOR_RESERVE(*str, strlen+len+1);
  1281. VECTOR_RESIZE(*str, strlen+len);
  1282. WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_FRONT(*str) + strlen, len+1, NULL, NULL);
  1283. *VECTOR_ITER_END(*str) = 0;
  1284. }
  1285. }
  1286. #endif