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
23 #define _WIN32_IE 0x501
25 #define _WIN32_IE 0x400
42 #ifndef AL_NO_UID_DEFS
43 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
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);
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);
70 #endif /* AL_NO_UID_DEFS */
81 #ifdef HAVE_SYS_SYSCONF_H
82 #include <sys/sysconf.h>
93 #elif defined(_WIN32_IE)
102 #include "alstring.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
)
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))
124 unsigned int regs
[4];
125 char str
[sizeof(unsigned int[4])];
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");
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
);
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)))
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))
169 char str
[sizeof(int[4])];
172 (__cpuid
)(cpuinf
[0].regs
, 0);
173 if(cpuinf
[0].regs
[0] == 0)
174 ERR("Failed to get CPUID\n");
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
);
196 (__cpuid
)(cpuinf
[0].regs
, 1);
197 if((cpuinf
[0].regs
[3]&(1<<25)))
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
;
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!"
230 /* Assume Neon support if compiled with it */
231 caps
|= CPU_CAP_NEON
;
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 *al_malloc(size_t alignment
, size_t size
)
248 #if defined(HAVE_ALIGNED_ALLOC)
249 size
= (size
+(alignment
-1))&~(alignment
-1);
250 return aligned_alloc(alignment
, size
);
251 #elif defined(HAVE_POSIX_MEMALIGN)
253 if(posix_memalign(&ret
, alignment
, size
) == 0)
256 #elif defined(HAVE__ALIGNED_MALLOC)
257 return _aligned_malloc(size
, alignment
);
259 char *ret
= malloc(size
+alignment
);
263 while(((ptrdiff_t)ret
&(alignment
-1)) != 0)
270 void *al_calloc(size_t alignment
, size_t size
)
272 void *ret
= al_malloc(alignment
, size
);
273 if(ret
) memset(ret
, 0, size
);
277 void al_free(void *ptr
)
279 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
281 #elif defined(HAVE__ALIGNED_MALLOC)
289 } while(*finder
== 0x55);
296 void SetMixerFPUMode(FPUCtl
*ctl
)
299 fegetenv(STATIC_CAST(fenv_t
, ctl
));
300 #if defined(__GNUC__) && defined(HAVE_SSE)
301 /* FIXME: Some fegetenv implementations can get the SSE environment too?
302 * How to tell when it does? */
303 if((CPUCapFlags
&CPU_CAP_SSE
))
304 __asm__
__volatile__("stmxcsr %0" : "=m" (*&ctl
->sse_state
));
308 fesetround(FE_TOWARDZERO
);
310 #if defined(__GNUC__) && defined(HAVE_SSE)
311 if((CPUCapFlags
&CPU_CAP_SSE
))
313 int sseState
= ctl
->sse_state
;
314 sseState
|= 0x6000; /* set round-to-zero */
315 sseState
|= 0x8000; /* set flush-to-zero */
316 if((CPUCapFlags
&CPU_CAP_SSE2
))
317 sseState
|= 0x0040; /* set denormals-are-zero */
318 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&sseState
));
322 #elif defined(HAVE___CONTROL87_2)
325 __control87_2(0, 0, &ctl
->state
, NULL
);
326 __control87_2(_RC_CHOP
, _MCW_RC
, &mode
, NULL
);
328 if((CPUCapFlags
&CPU_CAP_SSE
))
330 __control87_2(0, 0, NULL
, &ctl
->sse_state
);
331 __control87_2(_RC_CHOP
|_DN_FLUSH
, _MCW_RC
|_MCW_DN
, NULL
, &mode
);
335 #elif defined(HAVE__CONTROLFP)
337 ctl
->state
= _controlfp(0, 0);
338 (void)_controlfp(_RC_CHOP
, _MCW_RC
);
342 void RestoreFPUMode(const FPUCtl
*ctl
)
345 fesetenv(STATIC_CAST(fenv_t
, ctl
));
346 #if defined(__GNUC__) && defined(HAVE_SSE)
347 if((CPUCapFlags
&CPU_CAP_SSE
))
348 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&ctl
->sse_state
));
351 #elif defined(HAVE___CONTROL87_2)
354 __control87_2(ctl
->state
, _MCW_RC
, &mode
, NULL
);
356 if((CPUCapFlags
&CPU_CAP_SSE
))
357 __control87_2(ctl
->sse_state
, _MCW_RC
|_MCW_DN
, NULL
, &mode
);
360 #elif defined(HAVE__CONTROLFP)
362 _controlfp(ctl
->state
, _MCW_RC
);
367 static int StringSortCompare(const void *str1
, const void *str2
)
369 return al_string_cmp(*(const_al_string
*)str1
, *(const_al_string
*)str2
);
374 static WCHAR
*FromUTF8(const char *str
)
379 if((len
=MultiByteToWideChar(CP_UTF8
, 0, str
, -1, NULL
, 0)) > 0)
381 out
= calloc(sizeof(WCHAR
), len
);
382 MultiByteToWideChar(CP_UTF8
, 0, str
, -1, out
, len
);
388 void *LoadLib(const char *name
)
393 wname
= FromUTF8(name
);
395 ERR("Failed to convert UTF-8 filename: \"%s\"\n", name
);
398 hdl
= LoadLibraryW(wname
);
403 void CloseLib(void *handle
)
404 { FreeLibrary((HANDLE
)handle
); }
405 void *GetSymbol(void *handle
, const char *name
)
409 ret
= (void*)GetProcAddress((HANDLE
)handle
, name
);
411 ERR("Failed to load %s\n", name
);
415 WCHAR
*strdupW(const WCHAR
*str
)
425 ret
= calloc(sizeof(WCHAR
), len
+1);
427 memcpy(ret
, str
, sizeof(WCHAR
)*len
);
431 FILE *al_fopen(const char *fname
, const char *mode
)
433 WCHAR
*wname
=NULL
, *wmode
=NULL
;
436 wname
= FromUTF8(fname
);
437 wmode
= FromUTF8(mode
);
439 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname
);
441 ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode
);
443 file
= _wfopen(wname
, wmode
);
452 void al_print(const char *type
, const char *func
, const char *fmt
, ...)
459 vsnprintf(str
, sizeof(str
), fmt
, ap
);
462 str
[sizeof(str
)-1] = 0;
463 wstr
= FromUTF8(str
);
465 fprintf(LogFile
, "AL lib: %s %s: <UTF-8 error> %s", type
, func
, str
);
468 fprintf(LogFile
, "AL lib: %s %s: %ls", type
, func
, wstr
);
476 static inline int is_slash(int c
)
477 { return (c
== '\\' || c
== '/'); }
479 FILE *OpenDataFile(const char *fname
, const char *subdir
)
481 static const int ids
[2] = { CSIDL_APPDATA
, CSIDL_COMMON_APPDATA
};
482 WCHAR
*wname
=NULL
, *wsubdir
=NULL
;
486 wname
= FromUTF8(fname
);
489 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname
);
493 /* If the path is absolute, open it directly. */
494 if(wname
[0] != '\0' && wname
[1] == ':' && is_slash(wname
[2]))
496 f
= _wfopen(wname
, L
"rb");
497 if(f
) TRACE("Opened %s\n", fname
);
498 else WARN("Could not open %s\n", fname
);
503 /* Try the current directory first before the data directories. */
504 if((f
=_wfopen(wname
, L
"rb")) != NULL
)
506 TRACE("Opened %s\n", fname
);
511 wsubdir
= FromUTF8(subdir
);
514 ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir
);
519 for(i
= 0;i
< COUNTOF(ids
);i
++)
521 WCHAR buffer
[PATH_MAX
];
524 if(SHGetSpecialFolderPathW(NULL
, buffer
, ids
[i
], FALSE
) == FALSE
)
527 len
= lstrlenW(buffer
);
528 if(len
> 0 && is_slash(buffer
[len
-1]))
529 buffer
[--len
] = '\0';
530 _snwprintf(buffer
+len
, PATH_MAX
-len
, L
"/%ls/%ls", wsubdir
, wname
);
531 len
= lstrlenW(buffer
);
535 if(buffer
[len
] == '/')
539 if((f
=_wfopen(buffer
, L
"rb")) != NULL
)
541 al_string filepath
= AL_STRING_INIT_STATIC();
542 al_string_copy_wcstr(&filepath
, buffer
);
543 TRACE("Opened %s\n", al_string_get_cstr(filepath
));
544 al_string_deinit(&filepath
);
552 WARN("Could not open %s\\%s\n", subdir
, fname
);
557 static size_t strlenW(const WCHAR
*str
)
559 const WCHAR
*end
= str
;
564 static const WCHAR
*strchrW(const WCHAR
*str
, WCHAR ch
)
566 for(;*str
!= 0;++str
)
574 static const WCHAR
*strrchrW(const WCHAR
*str
, WCHAR ch
)
576 const WCHAR
*ret
= NULL
;
577 for(;*str
!= 0;++str
)
585 static const WCHAR
*strstrW(const WCHAR
*haystack
, const WCHAR
*needle
)
587 size_t len
= strlenW(needle
);
588 while(*haystack
!= 0)
590 if(CompareStringW(GetThreadLocale(), NORM_IGNORECASE
,
591 haystack
, len
, needle
, len
) == CSTR_EQUAL
)
596 } while(((*haystack
)&0xC000) == 0x8000);
602 /* Compares the filename in the find data with the match string. The match
603 * string may contain the "%r" marker to signifiy a sample rate (really any
604 * positive integer), "%%" to signify a single '%', or "%s" for a (non-greedy)
607 static int MatchFilter(const WCHAR
*match
, const WIN32_FIND_DATAW
*fdata
)
609 const WCHAR
*name
= fdata
->cFileName
;
613 const WCHAR
*p
= strchrW(match
, '%');
615 ret
= CompareStringW(GetThreadLocale(), NORM_IGNORECASE
,
616 match
, -1, name
, -1) == CSTR_EQUAL
;
620 ret
= lstrlenW(name
) >= len
;
622 ret
= CompareStringW(GetThreadLocale(), NORM_IGNORECASE
,
623 match
, len
, name
, len
) == CSTR_EQUAL
;
633 while(*name
>= '0' && *name
<= '9')
635 l
= l
*10 + (*name
-'0');
643 const WCHAR
*next
= p
+1;
644 if(*next
!= '\0' && *next
!= '%')
646 const WCHAR
*next_p
= strchrW(next
, '%');
650 m
= strstrW(name
, next
);
653 WCHAR
*tmp
= malloc((next_p
- next
+ 1) * 2);
654 memcpy(tmp
, next
, (next_p
- next
) * 2);
655 tmp
[next_p
- next
] = 0;
657 m
= strstrW(name
, tmp
);
666 if(next_p
) l
= next_p
- next
;
667 else l
= strlenW(next
);
679 } while(ret
&& match
&& *match
);
684 static void RecurseDirectorySearch(const char *path
, const WCHAR
*match
, vector_al_string
*results
)
686 WIN32_FIND_DATAW fdata
;
687 const WCHAR
*sep
, *p
;
693 /* Find the last directory separator and the next '%' marker in the match
695 sep
= strrchrW(match
, '\\');
696 p
= strchrW(match
, '%');
698 /* If there's no separator, test the files in the specified path against
699 * the match string, and add the results. */
702 al_string pathstr
= AL_STRING_INIT_STATIC();
705 TRACE("Searching %s for %ls\n", path
, match
);
707 al_string_append_cstr(&pathstr
, path
);
708 al_string_append_cstr(&pathstr
, "\\*.*");
709 wpath
= FromUTF8(al_string_get_cstr(pathstr
));
711 hdl
= FindFirstFileW(wpath
, &fdata
);
712 if(hdl
!= INVALID_HANDLE_VALUE
)
714 size_t base
= VECTOR_SIZE(*results
);
716 if(MatchFilter(match
, &fdata
))
718 al_string str
= AL_STRING_INIT_STATIC();
719 al_string_copy_cstr(&str
, path
);
720 al_string_append_char(&str
, '\\');
721 al_string_append_wcstr(&str
, fdata
.cFileName
);
722 TRACE("Got result %s\n", al_string_get_cstr(str
));
723 VECTOR_PUSH_BACK(*results
, str
);
725 } while(FindNextFileW(hdl
, &fdata
));
728 if(VECTOR_SIZE(*results
) > base
)
729 qsort(VECTOR_ITER_BEGIN(*results
)+base
, VECTOR_SIZE(*results
)-base
,
730 sizeof(VECTOR_FRONT(*results
)), StringSortCompare
);
734 al_string_deinit(&pathstr
);
739 /* If there's no '%' marker, or it's after the final separator, append the
740 * remaining directories to the path and recurse into it with the remaining
741 * filename portion. */
744 al_string npath
= AL_STRING_INIT_STATIC();
745 al_string_append_cstr(&npath
, path
);
746 al_string_append_char(&npath
, '\\');
747 al_string_append_wrange(&npath
, match
, sep
);
749 TRACE("Recursing into %s with %ls\n", al_string_get_cstr(npath
), sep
+1);
750 RecurseDirectorySearch(al_string_get_cstr(npath
), sep
+1, results
);
752 al_string_deinit(&npath
);
756 /* Look for the last separator before the '%' marker, and the first
757 * separator after it. */
758 sep
= strchrW(match
, '\\');
759 if(sep
-p
>= 0) sep
= NULL
;
762 const WCHAR
*next
= strchrW(sep
?sep
+1:match
, '\\');
765 al_string npath
= AL_STRING_INIT_STATIC();
766 WCHAR
*nwpath
, *nwmatch
;
768 /* Append up to the last directory before the one with a '%'. */
769 al_string_copy_cstr(&npath
, path
);
772 al_string_append_char(&npath
, '\\');
773 al_string_append_wrange(&npath
, match
, sep
);
775 al_string_append_cstr(&npath
, "\\*.*");
776 nwpath
= FromUTF8(al_string_get_cstr(npath
));
778 /* Take the directory name containing a '%' as a new string to
782 nwmatch
= calloc(2, next
-match
+1);
783 memcpy(nwmatch
, match
, (next
-match
)*2);
787 nwmatch
= calloc(2, next
-(sep
+1)+1);
788 memcpy(nwmatch
, sep
+1, (next
-(sep
+1))*2);
791 /* For each matching directory name, recurse into it with the
792 * remaining string. */
793 TRACE("Searching %s for %ls\n", al_string_get_cstr(npath
), nwmatch
);
794 hdl
= FindFirstFileW(nwpath
, &fdata
);
795 if(hdl
!= INVALID_HANDLE_VALUE
)
798 if(MatchFilter(nwmatch
, &fdata
))
800 al_string ndir
= AL_STRING_INIT_STATIC();
801 al_string_copy(&ndir
, npath
);
802 al_string_append_char(&ndir
, '\\');
803 al_string_append_wcstr(&ndir
, fdata
.cFileName
);
804 TRACE("Recursing %s with %ls\n", al_string_get_cstr(ndir
), next
+1);
805 RecurseDirectorySearch(al_string_get_cstr(ndir
), next
+1, results
);
806 al_string_deinit(&ndir
);
808 } while(FindNextFileW(hdl
, &fdata
));
814 al_string_deinit(&npath
);
821 vector_al_string
SearchDataFiles(const char *match
, const char *subdir
)
823 static const int ids
[2] = { CSIDL_APPDATA
, CSIDL_COMMON_APPDATA
};
824 static RefCount search_lock
;
825 vector_al_string results
= VECTOR_INIT_STATIC();
829 while(ATOMIC_EXCHANGE(uint
, &search_lock
, 1) == 1)
832 wmatch
= FromUTF8(match
);
835 ERR("Failed to convert UTF-8 filename: \"%s\"\n", match
);
838 for(i
= 0;wmatch
[i
];++i
)
844 /* If the path is absolute, use it directly. */
845 if(isalpha(wmatch
[0]) && wmatch
[1] == ':' && is_slash(wmatch
[2]))
847 char drv
[3] = { (char)wmatch
[0], ':', 0 };
848 RecurseDirectorySearch(drv
, wmatch
+3, &results
);
850 else if(wmatch
[0] == '\\' && wmatch
[1] == '\\' && wmatch
[2] == '?' && wmatch
[3] == '\\')
851 RecurseDirectorySearch("\\\\?", wmatch
+4, &results
);
854 al_string path
= AL_STRING_INIT_STATIC();
857 /* Search the app-local directory. */
858 if((cwdbuf
=_wgetenv(L
"ALSOFT_LOCAL_PATH")) && *cwdbuf
!= '\0')
860 al_string_copy_wcstr(&path
, cwdbuf
);
861 if(is_slash(VECTOR_BACK(path
)))
863 VECTOR_POP_BACK(path
);
864 *VECTOR_ITER_END(path
) = 0;
867 else if(!(cwdbuf
=_wgetcwd(NULL
, 0)))
868 al_string_copy_cstr(&path
, ".");
871 al_string_copy_wcstr(&path
, cwdbuf
);
872 if(is_slash(VECTOR_BACK(path
)))
874 VECTOR_POP_BACK(path
);
875 *VECTOR_ITER_END(path
) = 0;
879 #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
880 VECTOR_FOR_EACH(char, path
, FIX_SLASH
);
882 RecurseDirectorySearch(al_string_get_cstr(path
), wmatch
, &results
);
884 /* Search the local and global data dirs. */
885 for(i
= 0;i
< COUNTOF(ids
);i
++)
887 WCHAR buffer
[PATH_MAX
];
888 if(SHGetSpecialFolderPathW(NULL
, buffer
, ids
[i
], FALSE
) != FALSE
)
890 al_string_copy_wcstr(&path
, buffer
);
891 if(!is_slash(VECTOR_BACK(path
)))
892 al_string_append_char(&path
, '\\');
893 al_string_append_cstr(&path
, subdir
);
894 #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
895 VECTOR_FOR_EACH(char, path
, FIX_SLASH
);
898 RecurseDirectorySearch(al_string_get_cstr(path
), wmatch
, &results
);
902 al_string_deinit(&path
);
906 ATOMIC_STORE(&search_lock
, 0);
915 void *LoadLib(const char *name
)
921 handle
= dlopen(name
, RTLD_NOW
);
922 if((err
=dlerror()) != NULL
)
926 void CloseLib(void *handle
)
928 void *GetSymbol(void *handle
, const char *name
)
934 sym
= dlsym(handle
, name
);
935 if((err
=dlerror()) != NULL
)
937 WARN("Failed to load %s: %s\n", name
, err
);
943 #endif /* HAVE_DLFCN_H */
945 void al_print(const char *type
, const char *func
, const char *fmt
, ...)
950 fprintf(LogFile
, "AL lib: %s %s: ", type
, func
);
951 vfprintf(LogFile
, fmt
, ap
);
958 FILE *OpenDataFile(const char *fname
, const char *subdir
)
960 char buffer
[PATH_MAX
] = "";
961 const char *str
, *next
;
966 if((f
=al_fopen(fname
, "rb")) != NULL
)
968 TRACE("Opened %s\n", fname
);
971 WARN("Could not open %s\n", fname
);
975 if((f
=al_fopen(fname
, "rb")) != NULL
)
977 TRACE("Opened %s\n", fname
);
981 if((str
=getenv("XDG_DATA_HOME")) != NULL
&& str
[0] != '\0')
982 snprintf(buffer
, sizeof(buffer
), "%s/%s/%s", str
, subdir
, fname
);
983 else if((str
=getenv("HOME")) != NULL
&& str
[0] != '\0')
984 snprintf(buffer
, sizeof(buffer
), "%s/.local/share/%s/%s", str
, subdir
, fname
);
987 if((f
=al_fopen(buffer
, "rb")) != NULL
)
989 TRACE("Opened %s\n", buffer
);
994 if((str
=getenv("XDG_DATA_DIRS")) == NULL
|| str
[0] == '\0')
995 str
= "/usr/local/share/:/usr/share/";
998 while((str
=next
) != NULL
&& str
[0] != '\0')
1001 next
= strchr(str
, ':');
1011 if(len
> sizeof(buffer
)-1)
1012 len
= sizeof(buffer
)-1;
1013 strncpy(buffer
, str
, len
);
1015 snprintf(buffer
+len
, sizeof(buffer
)-len
, "/%s/%s", subdir
, fname
);
1017 if((f
=al_fopen(buffer
, "rb")) != NULL
)
1019 TRACE("Opened %s\n", buffer
);
1023 WARN("Could not open %s/%s\n", subdir
, fname
);
1029 static int MatchFilter(const char *name
, const char *match
)
1034 const char *p
= strchr(match
, '%');
1036 ret
= strcmp(match
, name
) == 0;
1039 size_t len
= p
-match
;
1040 ret
= strncmp(match
, name
, len
) == 0;
1050 ret
= strtoul(name
, &end
, 10) > 0;
1056 const char *next
= p
+1;
1057 if(*next
!= '\0' && *next
!= '%')
1059 const char *next_p
= strchr(next
, '%');
1063 m
= strstr(name
, next
);
1066 char *tmp
= malloc(next_p
- next
+ 1);
1067 memcpy(tmp
, next
, next_p
- next
);
1068 tmp
[next_p
- next
] = 0;
1070 m
= strstr(name
, tmp
);
1079 if(next_p
) l
= next_p
- next
;
1080 else l
= strlen(next
);
1092 } while(ret
&& match
&& *match
);
1097 static void RecurseDirectorySearch(const char *path
, const char *match
, vector_al_string
*results
)
1104 sep
= strrchr(match
, '/');
1105 p
= strchr(match
, '%');
1111 TRACE("Searching %s for %s\n", path
?path
:"/", match
);
1112 dir
= opendir(path
?path
:"/");
1115 size_t base
= VECTOR_SIZE(*results
);
1116 struct dirent
*dirent
;
1117 while((dirent
=readdir(dir
)) != NULL
)
1120 if(strcmp(dirent
->d_name
, ".") == 0 || strcmp(dirent
->d_name
, "..") == 0 ||
1121 !MatchFilter(dirent
->d_name
, match
))
1124 AL_STRING_INIT(str
);
1125 if(path
) al_string_copy_cstr(&str
, path
);
1126 al_string_append_char(&str
, '/');
1127 al_string_append_cstr(&str
, dirent
->d_name
);
1128 TRACE("Got result %s\n", al_string_get_cstr(str
));
1129 VECTOR_PUSH_BACK(*results
, str
);
1133 if(VECTOR_SIZE(*results
) > base
)
1134 qsort(VECTOR_ITER_BEGIN(*results
)+base
, VECTOR_SIZE(*results
)-base
,
1135 sizeof(VECTOR_FRONT(*results
)), StringSortCompare
);
1141 if(!p
|| p
-sep
>= 0)
1143 al_string npath
= AL_STRING_INIT_STATIC();
1144 if(path
) al_string_append_cstr(&npath
, path
);
1145 al_string_append_char(&npath
, '/');
1146 al_string_append_range(&npath
, match
, sep
);
1148 TRACE("Recursing into %s with %s\n", al_string_get_cstr(npath
), sep
+1);
1149 RecurseDirectorySearch(al_string_get_cstr(npath
), sep
+1, results
);
1151 al_string_deinit(&npath
);
1155 sep
= strchr(match
, '/');
1156 if(sep
-p
>= 0) sep
= NULL
;
1159 char *next
= strchr(sep
?sep
+1:match
, '/');
1162 al_string npath
= AL_STRING_INIT_STATIC();
1163 al_string nmatch
= AL_STRING_INIT_STATIC();
1164 const char *tomatch
;
1169 al_string_append_cstr(&npath
, path
?path
:"/.");
1174 if(path
) al_string_append_cstr(&npath
, path
);
1175 al_string_append_char(&npath
, '/');
1176 al_string_append_range(&npath
, match
, sep
);
1178 al_string_append_range(&nmatch
, sep
+1, next
);
1179 tomatch
= al_string_get_cstr(nmatch
);
1182 TRACE("Searching %s for %s\n", al_string_get_cstr(npath
), tomatch
);
1183 dir
= opendir(path
?path
:"/");
1186 al_string ndir
= AL_STRING_INIT_STATIC();
1187 struct dirent
*dirent
;
1189 while((dirent
=readdir(dir
)) != NULL
)
1191 if(strcmp(dirent
->d_name
, ".") == 0 || strcmp(dirent
->d_name
, "..") == 0 ||
1192 !MatchFilter(dirent
->d_name
, tomatch
))
1194 al_string_copy(&ndir
, npath
);
1195 al_string_append_char(&ndir
, '/');
1196 al_string_append_cstr(&ndir
, dirent
->d_name
);
1197 TRACE("Recursing %s with %s\n", al_string_get_cstr(ndir
), next
+1);
1198 RecurseDirectorySearch(al_string_get_cstr(ndir
), next
+1, results
);
1202 al_string_deinit(&ndir
);
1205 al_string_deinit(&nmatch
);
1206 al_string_deinit(&npath
);
1214 vector_al_string
SearchDataFiles(const char *match
, const char *subdir
)
1216 static RefCount search_lock
;
1217 vector_al_string results
= VECTOR_INIT_STATIC();
1219 while(ATOMIC_EXCHANGE(uint
, &search_lock
, 1) == 1)
1223 RecurseDirectorySearch(NULL
, match
+1, &results
);
1226 al_string path
= AL_STRING_INIT_STATIC();
1227 const char *str
, *next
;
1228 char cwdbuf
[PATH_MAX
];
1230 /* Search the app-local directory. */
1231 if((str
=getenv("ALSOFT_LOCAL_PATH")) && *str
!= '\0')
1233 strncpy(cwdbuf
, str
, sizeof(cwdbuf
)-1);
1234 cwdbuf
[sizeof(cwdbuf
)-1] = '\0';
1236 else if(!getcwd(cwdbuf
, sizeof(cwdbuf
)))
1237 strcpy(cwdbuf
, ".");
1238 RecurseDirectorySearch(cwdbuf
, match
, &results
);
1240 // Search local data dir
1241 if((str
=getenv("XDG_DATA_HOME")) != NULL
&& str
[0] != '\0')
1243 al_string_append_cstr(&path
, str
);
1244 al_string_append_char(&path
, '/');
1245 al_string_append_cstr(&path
, subdir
);
1247 else if((str
=getenv("HOME")) != NULL
&& str
[0] != '\0')
1249 al_string_append_cstr(&path
, str
);
1250 al_string_append_cstr(&path
, "/.local/share/");
1251 al_string_append_cstr(&path
, subdir
);
1253 if(!al_string_empty(path
))
1254 RecurseDirectorySearch(al_string_get_cstr(path
), match
, &results
);
1256 // Search global data dirs
1257 if((str
=getenv("XDG_DATA_DIRS")) == NULL
|| str
[0] == '\0')
1258 str
= "/usr/local/share/:/usr/share/";
1261 while((str
=next
) != NULL
&& str
[0] != '\0')
1263 next
= strchr(str
, ':');
1265 al_string_copy_cstr(&path
, str
);
1268 al_string_clear(&path
);
1269 al_string_append_range(&path
, str
, next
);
1272 if(!al_string_empty(path
))
1274 al_string_append_char(&path
, '/');
1275 al_string_append_cstr(&path
, subdir
);
1277 RecurseDirectorySearch(al_string_get_cstr(path
), match
, &results
);
1281 al_string_deinit(&path
);
1284 ATOMIC_STORE(&search_lock
, 0);
1292 void SetRTPriority(void)
1294 ALboolean failed
= AL_FALSE
;
1298 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
1299 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
1302 struct sched_param param
;
1303 /* Use the minimum real-time priority possible for now (on Linux this
1304 * should be 1 for SCHED_RR) */
1305 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
1306 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
1309 /* Real-time priority not available */
1310 failed
= (RTPrioLevel
>0);
1313 ERR("Failed to set priority level for thread\n");
1317 ALboolean
vector_reserve(char *ptr
, size_t base_size
, size_t obj_size
, size_t obj_count
, ALboolean exact
)
1319 vector_
*vecptr
= (vector_
*)ptr
;
1320 if((*vecptr
? (*vecptr
)->Capacity
: 0) < obj_count
)
1322 size_t old_size
= (*vecptr
? (*vecptr
)->Size
: 0);
1325 /* Use the next power-of-2 size if we don't need to allocate the exact
1326 * amount. This is preferred when regularly increasing the vector since
1327 * it means fewer reallocations. Though it means it also wastes some
1329 if(exact
== AL_FALSE
&& obj_count
< INT_MAX
)
1330 obj_count
= NextPowerOf2((ALuint
)obj_count
);
1332 /* Need to be explicit with the caller type's base size, because it
1333 * could have extra padding before the start of the array (that is,
1334 * sizeof(*vector_) may not equal base_size). */
1335 temp
= realloc(*vecptr
, base_size
+ obj_size
*obj_count
);
1336 if(temp
== NULL
) return AL_FALSE
;
1339 (*vecptr
)->Capacity
= obj_count
;
1340 (*vecptr
)->Size
= old_size
;
1345 ALboolean
vector_resize(char *ptr
, size_t base_size
, size_t obj_size
, size_t obj_count
)
1347 vector_
*vecptr
= (vector_
*)ptr
;
1348 if(*vecptr
|| obj_count
> 0)
1350 if(!vector_reserve((char*)vecptr
, base_size
, obj_size
, obj_count
, AL_TRUE
))
1352 (*vecptr
)->Size
= obj_count
;
1357 ALboolean
vector_insert(char *ptr
, size_t base_size
, size_t obj_size
, void *ins_pos
, const void *datstart
, const void *datend
)
1359 vector_
*vecptr
= (vector_
*)ptr
;
1360 if(datstart
!= datend
)
1362 ptrdiff_t ins_elem
= (*vecptr
? ((char*)ins_pos
- ((char*)(*vecptr
) + base_size
)) :
1363 ((char*)ins_pos
- (char*)NULL
)) /
1365 ptrdiff_t numins
= ((const char*)datend
- (const char*)datstart
) / obj_size
;
1368 if((size_t)numins
+ VECTOR_SIZE(*vecptr
) < (size_t)numins
||
1369 !vector_reserve((char*)vecptr
, base_size
, obj_size
, VECTOR_SIZE(*vecptr
)+numins
, AL_TRUE
))
1372 /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
1373 if((size_t)ins_elem
< (*vecptr
)->Size
)
1375 memmove((char*)(*vecptr
) + base_size
+ ((ins_elem
+numins
)*obj_size
),
1376 (char*)(*vecptr
) + base_size
+ ((ins_elem
)*obj_size
),
1377 ((*vecptr
)->Size
-ins_elem
)*obj_size
);
1379 memcpy((char*)(*vecptr
) + base_size
+ (ins_elem
*obj_size
),
1380 datstart
, numins
*obj_size
);
1381 (*vecptr
)->Size
+= numins
;
1387 extern inline void al_string_deinit(al_string
*str
);
1388 extern inline size_t al_string_length(const_al_string str
);
1389 extern inline ALboolean
al_string_empty(const_al_string str
);
1390 extern inline const al_string_char_type
*al_string_get_cstr(const_al_string str
);
1392 void al_string_clear(al_string
*str
)
1394 /* Reserve one more character than the total size of the string. This is to
1395 * ensure we have space to add a null terminator in the string data so it
1396 * can be used as a C-style string. */
1397 VECTOR_RESERVE(*str
, 1);
1398 VECTOR_RESIZE(*str
, 0);
1399 *VECTOR_ITER_END(*str
) = 0;
1402 static inline int al_string_compare(const al_string_char_type
*str1
, size_t str1len
,
1403 const al_string_char_type
*str2
, size_t str2len
)
1405 size_t complen
= (str1len
< str2len
) ? str1len
: str2len
;
1406 int ret
= memcmp(str1
, str2
, complen
);
1409 if(str1len
> str2len
) return 1;
1410 if(str1len
< str2len
) return -1;
1414 int al_string_cmp(const_al_string str1
, const_al_string str2
)
1416 return al_string_compare(&VECTOR_FRONT(str1
), al_string_length(str1
),
1417 &VECTOR_FRONT(str2
), al_string_length(str2
));
1419 int al_string_cmp_cstr(const_al_string str1
, const al_string_char_type
*str2
)
1421 return al_string_compare(&VECTOR_FRONT(str1
), al_string_length(str1
),
1422 str2
, strlen(str2
));
1425 void al_string_copy(al_string
*str
, const_al_string from
)
1427 size_t len
= al_string_length(from
);
1428 VECTOR_RESERVE(*str
, len
+1);
1429 VECTOR_RESIZE(*str
, 0);
1430 VECTOR_INSERT(*str
, VECTOR_ITER_END(*str
), VECTOR_ITER_BEGIN(from
), VECTOR_ITER_BEGIN(from
)+len
);
1431 *VECTOR_ITER_END(*str
) = 0;
1434 void al_string_copy_cstr(al_string
*str
, const al_string_char_type
*from
)
1436 size_t len
= strlen(from
);
1437 VECTOR_RESERVE(*str
, len
+1);
1438 VECTOR_RESIZE(*str
, 0);
1439 VECTOR_INSERT(*str
, VECTOR_ITER_END(*str
), from
, from
+len
);
1440 *VECTOR_ITER_END(*str
) = 0;
1443 void al_string_append_char(al_string
*str
, const al_string_char_type c
)
1445 VECTOR_RESERVE(*str
, al_string_length(*str
)+2);
1446 VECTOR_PUSH_BACK(*str
, c
);
1447 *VECTOR_ITER_END(*str
) = 0;
1450 void al_string_append_cstr(al_string
*str
, const al_string_char_type
*from
)
1452 size_t len
= strlen(from
);
1455 VECTOR_RESERVE(*str
, al_string_length(*str
)+len
+1);
1456 VECTOR_INSERT(*str
, VECTOR_ITER_END(*str
), from
, from
+len
);
1457 *VECTOR_ITER_END(*str
) = 0;
1461 void al_string_append_range(al_string
*str
, const al_string_char_type
*from
, const al_string_char_type
*to
)
1465 VECTOR_RESERVE(*str
, al_string_length(*str
)+(to
-from
)+1);
1466 VECTOR_INSERT(*str
, VECTOR_ITER_END(*str
), from
, to
);
1467 *VECTOR_ITER_END(*str
) = 0;
1472 void al_string_copy_wcstr(al_string
*str
, const wchar_t *from
)
1475 if((len
=WideCharToMultiByte(CP_UTF8
, 0, from
, -1, NULL
, 0, NULL
, NULL
)) > 0)
1477 VECTOR_RESERVE(*str
, len
);
1478 VECTOR_RESIZE(*str
, len
-1);
1479 WideCharToMultiByte(CP_UTF8
, 0, from
, -1, &VECTOR_FRONT(*str
), len
, NULL
, NULL
);
1480 *VECTOR_ITER_END(*str
) = 0;
1484 void al_string_append_wcstr(al_string
*str
, const wchar_t *from
)
1487 if((len
=WideCharToMultiByte(CP_UTF8
, 0, from
, -1, NULL
, 0, NULL
, NULL
)) > 0)
1489 size_t strlen
= al_string_length(*str
);
1490 VECTOR_RESERVE(*str
, strlen
+len
);
1491 VECTOR_RESIZE(*str
, strlen
+len
-1);
1492 WideCharToMultiByte(CP_UTF8
, 0, from
, -1, &VECTOR_FRONT(*str
) + strlen
, len
, NULL
, NULL
);
1493 *VECTOR_ITER_END(*str
) = 0;
1497 void al_string_append_wrange(al_string
*str
, const wchar_t *from
, const wchar_t *to
)
1500 if((len
=WideCharToMultiByte(CP_UTF8
, 0, from
, (int)(to
-from
), NULL
, 0, NULL
, NULL
)) > 0)
1502 size_t strlen
= al_string_length(*str
);
1503 VECTOR_RESERVE(*str
, strlen
+len
+1);
1504 VECTOR_RESIZE(*str
, strlen
+len
);
1505 WideCharToMultiByte(CP_UTF8
, 0, from
, (int)(to
-from
), &VECTOR_FRONT(*str
) + strlen
, len
+1, NULL
, NULL
);
1506 *VECTOR_ITER_END(*str
) = 0;