Don't assume the "real" output buffer follows the dry buffer
[openal-soft.git] / Alc / helpers.c
blob950a67bf43343ff7d26194c9f10877fee30136ef
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.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #ifdef _WIN32
22 #ifdef __MINGW32__
23 #define _WIN32_IE 0x501
24 #else
25 #define _WIN32_IE 0x400
26 #endif
27 #endif
29 #include "config.h"
31 #include <stdlib.h>
32 #include <time.h>
33 #include <errno.h>
34 #include <stdarg.h>
35 #ifdef HAVE_MALLOC_H
36 #include <malloc.h>
37 #endif
38 #ifdef HAVE_DIRENT_H
39 #include <dirent.h>
40 #endif
42 #ifndef AL_NO_UID_DEFS
43 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
44 #define INITGUID
45 #include <windows.h>
46 #ifdef HAVE_GUIDDEF_H
47 #include <guiddef.h>
48 #else
49 #include <initguid.h>
50 #endif
52 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
53 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
55 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
57 DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
58 DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
59 DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
60 DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
61 DEFINE_GUID(IID_IAudioCaptureClient, 0xc8adbd64, 0xe71e, 0x48a0, 0xa4,0xde, 0x18,0x5c,0x39,0x5c,0xd3,0x17);
63 #ifdef HAVE_MMDEVAPI
64 #include <devpropdef.h>
65 #include <propkeydef.h>
66 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
67 DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_FormFactor, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 0);
68 #endif
69 #endif
70 #endif /* AL_NO_UID_DEFS */
72 #ifdef HAVE_DLFCN_H
73 #include <dlfcn.h>
74 #endif
75 #ifdef HAVE_INTRIN_H
76 #include <intrin.h>
77 #endif
78 #ifdef HAVE_CPUID_H
79 #include <cpuid.h>
80 #endif
81 #ifdef HAVE_SYS_SYSCONF_H
82 #include <sys/sysconf.h>
83 #endif
84 #ifdef HAVE_FLOAT_H
85 #include <float.h>
86 #endif
87 #ifdef HAVE_IEEEFP_H
88 #include <ieeefp.h>
89 #endif
91 #ifndef _WIN32
92 #include <unistd.h>
93 #elif defined(_WIN32_IE)
94 #include <shlobj.h>
95 #endif
97 #include "alMain.h"
98 #include "alu.h"
99 #include "atomic.h"
100 #include "uintmap.h"
101 #include "vector.h"
102 #include "alstring.h"
103 #include "compat.h"
104 #include "threads.h"
107 extern inline ALuint NextPowerOf2(ALuint value);
108 extern inline ALint fastf2i(ALfloat f);
109 extern inline ALuint fastf2u(ALfloat f);
112 ALuint CPUCapFlags = 0;
115 void FillCPUCaps(ALuint capfilter)
117 ALuint caps = 0;
119 /* FIXME: We really should get this for all available CPUs in case different
120 * CPUs have different caps (is that possible on one machine?). */
121 #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
122 defined(_M_IX86) || defined(_M_X64))
123 union {
124 unsigned int regs[4];
125 char str[sizeof(unsigned int[4])];
126 } cpuinf[3];
128 if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
129 ERR("Failed to get CPUID\n");
130 else
132 unsigned int maxfunc = cpuinf[0].regs[0];
133 unsigned int maxextfunc = 0;
135 if(__get_cpuid(0x80000000, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
136 maxextfunc = cpuinf[0].regs[0];
137 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
139 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
140 if(maxextfunc >= 0x80000004 &&
141 __get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
142 __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
143 __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
144 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
146 if(maxfunc >= 1 &&
147 __get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
149 if((cpuinf[0].regs[3]&(1<<25)))
151 caps |= CPU_CAP_SSE;
152 if((cpuinf[0].regs[3]&(1<<26)))
154 caps |= CPU_CAP_SSE2;
155 if((cpuinf[0].regs[2]&(1<<0)))
157 caps |= CPU_CAP_SSE3;
158 if((cpuinf[0].regs[2]&(1<<19)))
159 caps |= CPU_CAP_SSE4_1;
165 #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
166 defined(_M_IX86) || defined(_M_X64))
167 union {
168 int regs[4];
169 char str[sizeof(int[4])];
170 } cpuinf[3];
172 (__cpuid)(cpuinf[0].regs, 0);
173 if(cpuinf[0].regs[0] == 0)
174 ERR("Failed to get CPUID\n");
175 else
177 unsigned int maxfunc = cpuinf[0].regs[0];
178 unsigned int maxextfunc;
180 (__cpuid)(cpuinf[0].regs, 0x80000000);
181 maxextfunc = cpuinf[0].regs[0];
183 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
185 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
186 if(maxextfunc >= 0x80000004)
188 (__cpuid)(cpuinf[0].regs, 0x80000002);
189 (__cpuid)(cpuinf[1].regs, 0x80000003);
190 (__cpuid)(cpuinf[2].regs, 0x80000004);
191 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
194 if(maxfunc >= 1)
196 (__cpuid)(cpuinf[0].regs, 1);
197 if((cpuinf[0].regs[3]&(1<<25)))
199 caps |= CPU_CAP_SSE;
200 if((cpuinf[0].regs[3]&(1<<26)))
202 caps |= CPU_CAP_SSE2;
203 if((cpuinf[0].regs[2]&(1<<0)))
205 caps |= CPU_CAP_SSE3;
206 if((cpuinf[0].regs[2]&(1<<19)))
207 caps |= CPU_CAP_SSE4_1;
213 #else
214 /* Assume support for whatever's supported if we can't check for it */
215 #if defined(HAVE_SSE4_1)
216 #warning "Assuming SSE 4.1 run-time support!"
217 caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
218 #elif defined(HAVE_SSE3)
219 #warning "Assuming SSE 3 run-time support!"
220 caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
221 #elif defined(HAVE_SSE2)
222 #warning "Assuming SSE 2 run-time support!"
223 caps |= CPU_CAP_SSE | CPU_CAP_SSE2;
224 #elif defined(HAVE_SSE)
225 #warning "Assuming SSE run-time support!"
226 caps |= CPU_CAP_SSE;
227 #endif
228 #endif
229 #ifdef HAVE_NEON
230 /* Assume Neon support if compiled with it */
231 caps |= CPU_CAP_NEON;
232 #endif
234 TRACE("Extensions:%s%s%s%s%s%s\n",
235 ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
236 ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
237 ((capfilter&CPU_CAP_SSE3) ? ((caps&CPU_CAP_SSE3) ? " +SSE3" : " -SSE3") : ""),
238 ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
239 ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +Neon" : " -Neon") : ""),
240 ((!capfilter) ? " -none-" : "")
242 CPUCapFlags = caps & capfilter;
246 void SetMixerFPUMode(FPUCtl *ctl)
248 #ifdef HAVE_FENV_H
249 fegetenv(STATIC_CAST(fenv_t, ctl));
250 #if defined(__GNUC__) && defined(HAVE_SSE)
251 /* FIXME: Some fegetenv implementations can get the SSE environment too?
252 * How to tell when it does? */
253 if((CPUCapFlags&CPU_CAP_SSE))
254 __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
255 #endif
257 #ifdef FE_TOWARDZERO
258 fesetround(FE_TOWARDZERO);
259 #endif
260 #if defined(__GNUC__) && defined(HAVE_SSE)
261 if((CPUCapFlags&CPU_CAP_SSE))
263 int sseState = ctl->sse_state;
264 sseState |= 0x6000; /* set round-to-zero */
265 sseState |= 0x8000; /* set flush-to-zero */
266 if((CPUCapFlags&CPU_CAP_SSE2))
267 sseState |= 0x0040; /* set denormals-are-zero */
268 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
270 #endif
272 #elif defined(HAVE___CONTROL87_2)
274 int mode;
275 __control87_2(0, 0, &ctl->state, NULL);
276 __control87_2(_RC_CHOP, _MCW_RC, &mode, NULL);
277 #ifdef HAVE_SSE
278 if((CPUCapFlags&CPU_CAP_SSE))
280 __control87_2(0, 0, NULL, &ctl->sse_state);
281 __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
283 #endif
285 #elif defined(HAVE__CONTROLFP)
287 ctl->state = _controlfp(0, 0);
288 (void)_controlfp(_RC_CHOP, _MCW_RC);
289 #endif
292 void RestoreFPUMode(const FPUCtl *ctl)
294 #ifdef HAVE_FENV_H
295 fesetenv(STATIC_CAST(fenv_t, ctl));
296 #if defined(__GNUC__) && defined(HAVE_SSE)
297 if((CPUCapFlags&CPU_CAP_SSE))
298 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
299 #endif
301 #elif defined(HAVE___CONTROL87_2)
303 int mode;
304 __control87_2(ctl->state, _MCW_RC, &mode, NULL);
305 #ifdef HAVE_SSE
306 if((CPUCapFlags&CPU_CAP_SSE))
307 __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
308 #endif
310 #elif defined(HAVE__CONTROLFP)
312 _controlfp(ctl->state, _MCW_RC);
313 #endif
317 static int StringSortCompare(const void *str1, const void *str2)
319 return al_string_cmp(*(const_al_string*)str1, *(const_al_string*)str2);
322 #ifdef _WIN32
324 static WCHAR *FromUTF8(const char *str)
326 WCHAR *out = NULL;
327 int len;
329 if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
331 out = calloc(sizeof(WCHAR), len);
332 MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
334 return out;
338 void *LoadLib(const char *name)
340 HANDLE hdl = NULL;
341 WCHAR *wname;
343 wname = FromUTF8(name);
344 if(!wname)
345 ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
346 else
348 hdl = LoadLibraryW(wname);
349 free(wname);
351 return hdl;
353 void CloseLib(void *handle)
354 { FreeLibrary((HANDLE)handle); }
355 void *GetSymbol(void *handle, const char *name)
357 void *ret;
359 ret = (void*)GetProcAddress((HANDLE)handle, name);
360 if(ret == NULL)
361 ERR("Failed to load %s\n", name);
362 return ret;
365 WCHAR *strdupW(const WCHAR *str)
367 const WCHAR *n;
368 WCHAR *ret;
369 size_t len;
371 n = str;
372 while(*n) n++;
373 len = n - str;
375 ret = calloc(sizeof(WCHAR), len+1);
376 if(ret != NULL)
377 memcpy(ret, str, sizeof(WCHAR)*len);
378 return ret;
381 FILE *al_fopen(const char *fname, const char *mode)
383 WCHAR *wname=NULL, *wmode=NULL;
384 FILE *file = NULL;
386 wname = FromUTF8(fname);
387 wmode = FromUTF8(mode);
388 if(!wname)
389 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
390 else if(!wmode)
391 ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
392 else
393 file = _wfopen(wname, wmode);
395 free(wname);
396 free(wmode);
398 return file;
402 void al_print(const char *type, const char *func, const char *fmt, ...)
404 char str[1024];
405 WCHAR *wstr;
406 va_list ap;
408 va_start(ap, fmt);
409 vsnprintf(str, sizeof(str), fmt, ap);
410 va_end(ap);
412 str[sizeof(str)-1] = 0;
413 wstr = FromUTF8(str);
414 if(!wstr)
415 fprintf(LogFile, "AL lib: %s %s: <UTF-8 error> %s", type, func, str);
416 else
418 fprintf(LogFile, "AL lib: %s %s: %ls", type, func, wstr);
419 free(wstr);
420 wstr = NULL;
422 fflush(LogFile);
426 static inline int is_slash(int c)
427 { return (c == '\\' || c == '/'); }
429 static void DirectorySearch(const char *path, const char *ext, vector_al_string *results)
431 al_string pathstr = AL_STRING_INIT_STATIC();
432 WIN32_FIND_DATAW fdata;
433 WCHAR *wpath;
434 HANDLE hdl;
436 al_string_copy_cstr(&pathstr, path);
437 al_string_append_cstr(&pathstr, "\\*");
438 al_string_append_cstr(&pathstr, ext);
440 TRACE("Searching %s\n", al_string_get_cstr(pathstr));
442 wpath = FromUTF8(al_string_get_cstr(pathstr));
444 hdl = FindFirstFileW(wpath, &fdata);
445 if(hdl != INVALID_HANDLE_VALUE)
447 size_t base = VECTOR_SIZE(*results);
448 do {
449 al_string str = AL_STRING_INIT_STATIC();
450 al_string_copy_cstr(&str, path);
451 al_string_append_char(&str, '\\');
452 al_string_append_wcstr(&str, fdata.cFileName);
453 TRACE("Got result %s\n", al_string_get_cstr(str));
454 VECTOR_PUSH_BACK(*results, str);
455 } while(FindNextFileW(hdl, &fdata));
456 FindClose(hdl);
458 if(VECTOR_SIZE(*results) > base)
459 qsort(VECTOR_BEGIN(*results)+base, VECTOR_SIZE(*results)-base,
460 sizeof(VECTOR_FRONT(*results)), StringSortCompare);
463 free(wpath);
464 al_string_deinit(&pathstr);
467 vector_al_string SearchDataFiles(const char *ext, const char *subdir)
469 static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
470 static RefCount search_lock;
471 vector_al_string results = VECTOR_INIT_STATIC();
472 size_t i;
474 while(ATOMIC_EXCHANGE(uint, &search_lock, 1) == 1)
475 althrd_yield();
477 /* If the path is absolute, use it directly. */
478 if(isalpha(subdir[0]) && subdir[1] == ':' && is_slash(subdir[2]))
480 al_string path = AL_STRING_INIT_STATIC();
481 al_string_copy_cstr(&path, subdir);
482 #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
483 VECTOR_FOR_EACH(char, path, FIX_SLASH);
484 #undef FIX_SLASH
486 DirectorySearch(al_string_get_cstr(path), ext, &results);
488 al_string_deinit(&path);
490 else if(subdir[0] == '\\' && subdir[1] == '\\' && subdir[2] == '?' && subdir[3] == '\\')
491 DirectorySearch(subdir, ext, &results);
492 else
494 al_string path = AL_STRING_INIT_STATIC();
495 WCHAR *cwdbuf;
497 /* Search the app-local directory. */
498 if((cwdbuf=_wgetenv(L"ALSOFT_LOCAL_PATH")) && *cwdbuf != '\0')
500 al_string_copy_wcstr(&path, cwdbuf);
501 if(is_slash(VECTOR_BACK(path)))
503 VECTOR_POP_BACK(path);
504 *VECTOR_END(path) = 0;
507 else if(!(cwdbuf=_wgetcwd(NULL, 0)))
508 al_string_copy_cstr(&path, ".");
509 else
511 al_string_copy_wcstr(&path, cwdbuf);
512 if(is_slash(VECTOR_BACK(path)))
514 VECTOR_POP_BACK(path);
515 *VECTOR_END(path) = 0;
517 free(cwdbuf);
519 #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
520 VECTOR_FOR_EACH(char, path, FIX_SLASH);
521 #undef FIX_SLASH
522 DirectorySearch(al_string_get_cstr(path), ext, &results);
524 /* Search the local and global data dirs. */
525 for(i = 0;i < COUNTOF(ids);i++)
527 WCHAR buffer[PATH_MAX];
528 if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) != FALSE)
530 al_string_copy_wcstr(&path, buffer);
531 if(!is_slash(VECTOR_BACK(path)))
532 al_string_append_char(&path, '\\');
533 al_string_append_cstr(&path, subdir);
534 #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
535 VECTOR_FOR_EACH(char, path, FIX_SLASH);
536 #undef FIX_SLASH
538 DirectorySearch(al_string_get_cstr(path), ext, &results);
542 al_string_deinit(&path);
545 ATOMIC_STORE(&search_lock, 0);
547 return results;
550 #else
552 #ifdef HAVE_DLFCN_H
554 void *LoadLib(const char *name)
556 const char *err;
557 void *handle;
559 dlerror();
560 handle = dlopen(name, RTLD_NOW);
561 if((err=dlerror()) != NULL)
562 handle = NULL;
563 return handle;
565 void CloseLib(void *handle)
566 { dlclose(handle); }
567 void *GetSymbol(void *handle, const char *name)
569 const char *err;
570 void *sym;
572 dlerror();
573 sym = dlsym(handle, name);
574 if((err=dlerror()) != NULL)
576 WARN("Failed to load %s: %s\n", name, err);
577 sym = NULL;
579 return sym;
582 #endif /* HAVE_DLFCN_H */
584 void al_print(const char *type, const char *func, const char *fmt, ...)
586 va_list ap;
588 va_start(ap, fmt);
589 fprintf(LogFile, "AL lib: %s %s: ", type, func);
590 vfprintf(LogFile, fmt, ap);
591 va_end(ap);
593 fflush(LogFile);
597 static void DirectorySearch(const char *path, const char *ext, vector_al_string *results)
599 size_t extlen = strlen(ext);
600 DIR *dir;
602 TRACE("Searching %s for *%s\n", path, ext);
603 dir = opendir(path);
604 if(dir != NULL)
606 size_t base = VECTOR_SIZE(*results);
607 struct dirent *dirent;
608 while((dirent=readdir(dir)) != NULL)
610 al_string str;
611 size_t len;
612 if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
613 continue;
615 len = strlen(dirent->d_name);
616 if(!(len > extlen))
617 continue;
618 if(strcasecmp(dirent->d_name+len-extlen, ext) != 0)
619 continue;
621 AL_STRING_INIT(str);
622 al_string_copy_cstr(&str, path);
623 if(VECTOR_BACK(str) != '/')
624 al_string_append_char(&str, '/');
625 al_string_append_cstr(&str, dirent->d_name);
626 TRACE("Got result %s\n", al_string_get_cstr(str));
627 VECTOR_PUSH_BACK(*results, str);
629 closedir(dir);
631 if(VECTOR_SIZE(*results) > base)
632 qsort(VECTOR_BEGIN(*results)+base, VECTOR_SIZE(*results)-base,
633 sizeof(VECTOR_FRONT(*results)), StringSortCompare);
637 vector_al_string SearchDataFiles(const char *ext, const char *subdir)
639 static RefCount search_lock;
640 vector_al_string results = VECTOR_INIT_STATIC();
642 while(ATOMIC_EXCHANGE(uint, &search_lock, 1) == 1)
643 althrd_yield();
645 if(subdir[0] == '/')
646 DirectorySearch(subdir, ext, &results);
647 else
649 al_string path = AL_STRING_INIT_STATIC();
650 const char *str, *next;
651 char cwdbuf[PATH_MAX];
653 /* Search the app-local directory. */
654 if((str=getenv("ALSOFT_LOCAL_PATH")) && *str != '\0')
655 DirectorySearch(str, ext, &results);
656 else if(getcwd(cwdbuf, sizeof(cwdbuf)))
657 DirectorySearch(cwdbuf, ext, &results);
658 else
659 DirectorySearch(".", ext, &results);
661 // Search local data dir
662 if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
664 al_string_copy_cstr(&path, str);
665 if(VECTOR_BACK(path) != '/')
666 al_string_append_char(&path, '/');
667 al_string_append_cstr(&path, subdir);
668 DirectorySearch(al_string_get_cstr(path), ext, &results);
670 else if((str=getenv("HOME")) != NULL && str[0] != '\0')
672 al_string_copy_cstr(&path, str);
673 if(VECTOR_BACK(path) == '/')
675 VECTOR_POP_BACK(path);
676 *VECTOR_END(path) = 0;
678 al_string_append_cstr(&path, "/.local/share/");
679 al_string_append_cstr(&path, subdir);
680 DirectorySearch(al_string_get_cstr(path), ext, &results);
683 // Search global data dirs
684 if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
685 str = "/usr/local/share/:/usr/share/";
687 next = str;
688 while((str=next) != NULL && str[0] != '\0')
690 next = strchr(str, ':');
691 if(!next)
692 al_string_copy_cstr(&path, str);
693 else
695 al_string_copy_range(&path, str, next);
696 ++next;
698 if(!al_string_empty(path))
700 if(VECTOR_BACK(path) != '/')
701 al_string_append_char(&path, '/');
702 al_string_append_cstr(&path, subdir);
704 DirectorySearch(al_string_get_cstr(path), ext, &results);
708 al_string_deinit(&path);
711 ATOMIC_STORE(&search_lock, 0);
713 return results;
716 #endif
719 void SetRTPriority(void)
721 ALboolean failed = AL_FALSE;
723 #ifdef _WIN32
724 if(RTPrioLevel > 0)
725 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
726 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
727 if(RTPrioLevel > 0)
729 struct sched_param param;
730 /* Use the minimum real-time priority possible for now (on Linux this
731 * should be 1 for SCHED_RR) */
732 param.sched_priority = sched_get_priority_min(SCHED_RR);
733 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
735 #else
736 /* Real-time priority not available */
737 failed = (RTPrioLevel>0);
738 #endif
739 if(failed)
740 ERR("Failed to set priority level for thread\n");
744 ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, size_t obj_count, ALboolean exact)
746 vector_ *vecptr = (vector_*)ptr;
747 if((*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
749 size_t old_size = (*vecptr ? (*vecptr)->Size : 0);
750 void *temp;
752 /* Use the next power-of-2 size if we don't need to allocate the exact
753 * amount. This is preferred when regularly increasing the vector since
754 * it means fewer reallocations. Though it means it also wastes some
755 * memory. */
756 if(exact == AL_FALSE && obj_count < INT_MAX)
757 obj_count = NextPowerOf2((ALuint)obj_count);
759 /* Need to be explicit with the caller type's base size, because it
760 * could have extra padding before the start of the array (that is,
761 * sizeof(*vector_) may not equal base_size). */
762 temp = realloc(*vecptr, base_size + obj_size*obj_count);
763 if(temp == NULL) return AL_FALSE;
765 *vecptr = temp;
766 (*vecptr)->Capacity = obj_count;
767 (*vecptr)->Size = old_size;
769 return AL_TRUE;
772 ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, size_t obj_count)
774 vector_ *vecptr = (vector_*)ptr;
775 if(*vecptr || obj_count > 0)
777 if(!vector_reserve((char*)vecptr, base_size, obj_size, obj_count, AL_TRUE))
778 return AL_FALSE;
779 (*vecptr)->Size = obj_count;
781 return AL_TRUE;
784 ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend)
786 vector_ *vecptr = (vector_*)ptr;
787 if(datstart != datend)
789 ptrdiff_t ins_elem = (*vecptr ? ((char*)ins_pos - ((char*)(*vecptr) + base_size)) :
790 ((char*)ins_pos - (char*)NULL)) /
791 obj_size;
792 ptrdiff_t numins = ((const char*)datend - (const char*)datstart) / obj_size;
794 assert(numins > 0);
795 if((size_t)numins + VECTOR_SIZE(*vecptr) < (size_t)numins ||
796 !vector_reserve((char*)vecptr, base_size, obj_size, VECTOR_SIZE(*vecptr)+numins, AL_TRUE))
797 return AL_FALSE;
799 /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
800 if((size_t)ins_elem < (*vecptr)->Size)
802 memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
803 (char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
804 ((*vecptr)->Size-ins_elem)*obj_size);
806 memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
807 datstart, numins*obj_size);
808 (*vecptr)->Size += numins;
810 return AL_TRUE;
814 extern inline void al_string_deinit(al_string *str);
815 extern inline size_t al_string_length(const_al_string str);
816 extern inline ALboolean al_string_empty(const_al_string str);
817 extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
819 void al_string_clear(al_string *str)
821 if(!al_string_empty(*str))
823 /* Reserve one more character than the total size of the string. This
824 * is to ensure we have space to add a null terminator in the string
825 * data so it can be used as a C-style string.
827 VECTOR_RESERVE(*str, 1);
828 VECTOR_RESIZE(*str, 0);
829 *VECTOR_END(*str) = 0;
833 static inline int al_string_compare(const al_string_char_type *str1, size_t str1len,
834 const al_string_char_type *str2, size_t str2len)
836 size_t complen = (str1len < str2len) ? str1len : str2len;
837 int ret = memcmp(str1, str2, complen);
838 if(ret == 0)
840 if(str1len > str2len) return 1;
841 if(str1len < str2len) return -1;
843 return ret;
845 int al_string_cmp(const_al_string str1, const_al_string str2)
847 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
848 &VECTOR_FRONT(str2), al_string_length(str2));
850 int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
852 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
853 str2, strlen(str2));
856 void al_string_copy(al_string *str, const_al_string from)
858 size_t len = al_string_length(from);
859 VECTOR_RESERVE(*str, len+1);
860 VECTOR_RESIZE(*str, 0);
861 VECTOR_INSERT(*str, VECTOR_END(*str), VECTOR_BEGIN(from), VECTOR_BEGIN(from)+len);
862 *VECTOR_END(*str) = 0;
865 void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
867 size_t len = strlen(from);
868 VECTOR_RESERVE(*str, len+1);
869 VECTOR_RESIZE(*str, 0);
870 VECTOR_INSERT(*str, VECTOR_END(*str), from, from+len);
871 *VECTOR_END(*str) = 0;
874 void al_string_copy_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
876 size_t len = to - from;
877 VECTOR_RESERVE(*str, len+1);
878 VECTOR_RESIZE(*str, 0);
879 VECTOR_INSERT(*str, VECTOR_END(*str), from, to);
880 *VECTOR_END(*str) = 0;
883 void al_string_append_char(al_string *str, const al_string_char_type c)
885 VECTOR_RESERVE(*str, al_string_length(*str)+2);
886 VECTOR_PUSH_BACK(*str, c);
887 *VECTOR_END(*str) = 0;
890 void al_string_append_cstr(al_string *str, const al_string_char_type *from)
892 size_t len = strlen(from);
893 if(len != 0)
895 VECTOR_RESERVE(*str, al_string_length(*str)+len+1);
896 VECTOR_INSERT(*str, VECTOR_END(*str), from, from+len);
897 *VECTOR_END(*str) = 0;
901 void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
903 if(to != from)
905 VECTOR_RESERVE(*str, al_string_length(*str)+(to-from)+1);
906 VECTOR_INSERT(*str, VECTOR_END(*str), from, to);
907 *VECTOR_END(*str) = 0;
911 #ifdef _WIN32
912 void al_string_copy_wcstr(al_string *str, const wchar_t *from)
914 int len;
915 if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
917 VECTOR_RESERVE(*str, len);
918 VECTOR_RESIZE(*str, len-1);
919 WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
920 *VECTOR_END(*str) = 0;
924 void al_string_append_wcstr(al_string *str, const wchar_t *from)
926 int len;
927 if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
929 size_t strlen = al_string_length(*str);
930 VECTOR_RESERVE(*str, strlen+len);
931 VECTOR_RESIZE(*str, strlen+len-1);
932 WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str) + strlen, len, NULL, NULL);
933 *VECTOR_END(*str) = 0;
937 void al_string_append_wrange(al_string *str, const wchar_t *from, const wchar_t *to)
939 int len;
940 if((len=WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), NULL, 0, NULL, NULL)) > 0)
942 size_t strlen = al_string_length(*str);
943 VECTOR_RESERVE(*str, strlen+len+1);
944 VECTOR_RESIZE(*str, strlen+len);
945 WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_FRONT(*str) + strlen, len+1, NULL, NULL);
946 *VECTOR_END(*str) = 0;
949 #endif