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
44 #include <sys/types.h>
45 #include <sys/sysctl.h>
48 #ifndef AL_NO_UID_DEFS
49 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
58 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
59 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
61 DEFINE_GUID(IID_IDirectSoundNotify
, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
63 DEFINE_GUID(CLSID_MMDeviceEnumerator
, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
64 DEFINE_GUID(IID_IMMDeviceEnumerator
, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
65 DEFINE_GUID(IID_IAudioClient
, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
66 DEFINE_GUID(IID_IAudioRenderClient
, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
67 DEFINE_GUID(IID_IAudioCaptureClient
, 0xc8adbd64, 0xe71e, 0x48a0, 0xa4,0xde, 0x18,0x5c,0x39,0x5c,0xd3,0x17);
71 #include <devpropdef.h>
72 #include <propkeydef.h>
73 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName
, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
74 DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_FormFactor
, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 0);
75 DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_GUID
, 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x23,0xe0, 0xc0,0xff,0xee,0x7f,0x0e, 4 );
78 #endif /* AL_NO_UID_DEFS */
89 #ifdef HAVE_SYS_SYSCONF_H
90 #include <sys/sysconf.h>
100 #include <sys/types.h>
101 #include <sys/stat.h>
102 #include <sys/mman.h>
105 #elif defined(_WIN32_IE)
111 #include "cpu_caps.h"
112 #include "fpu_modes.h"
116 #include "alstring.h"
121 extern inline ALuint
NextPowerOf2(ALuint value
);
122 extern inline size_t RoundUp(size_t value
, size_t r
);
123 extern inline ALint
fastf2i(ALfloat f
);
125 #if defined(HAVE_BITSCANFORWARD64_INTRINSIC)
126 extern inline int msvc64_ctz64(ALuint64 v
);
127 #elif defined(HAVE_BITSCANFORWARD_INTRINSIC)
128 extern inline int msvc_ctz64(ALuint64 v
);
130 extern inline int fallback_popcnt64(ALuint64 v
);
131 extern inline int fallback_ctz64(ALuint64 value
);
138 void FillCPUCaps(int capfilter
)
142 /* FIXME: We really should get this for all available CPUs in case different
143 * CPUs have different caps (is that possible on one machine?). */
144 #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
145 defined(_M_IX86) || defined(_M_X64))
147 unsigned int regs
[4];
148 char str
[sizeof(unsigned int[4])];
151 if(!__get_cpuid(0, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
152 ERR("Failed to get CPUID\n");
155 unsigned int maxfunc
= cpuinf
[0].regs
[0];
156 unsigned int maxextfunc
= 0;
158 if(__get_cpuid(0x80000000, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
159 maxextfunc
= cpuinf
[0].regs
[0];
160 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc
, maxextfunc
);
162 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf
[0].str
+4, cpuinf
[0].str
+12, cpuinf
[0].str
+8);
163 if(maxextfunc
>= 0x80000004 &&
164 __get_cpuid(0x80000002, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]) &&
165 __get_cpuid(0x80000003, &cpuinf
[1].regs
[0], &cpuinf
[1].regs
[1], &cpuinf
[1].regs
[2], &cpuinf
[1].regs
[3]) &&
166 __get_cpuid(0x80000004, &cpuinf
[2].regs
[0], &cpuinf
[2].regs
[1], &cpuinf
[2].regs
[2], &cpuinf
[2].regs
[3]))
167 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf
[0].str
, cpuinf
[1].str
, cpuinf
[2].str
);
170 __get_cpuid(1, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
172 if((cpuinf
[0].regs
[3]&(1<<25)))
175 if((cpuinf
[0].regs
[3]&(1<<26)))
177 caps
|= CPU_CAP_SSE2
;
178 if((cpuinf
[0].regs
[2]&(1<<0)))
180 caps
|= CPU_CAP_SSE3
;
181 if((cpuinf
[0].regs
[2]&(1<<19)))
182 caps
|= CPU_CAP_SSE4_1
;
188 #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
189 defined(_M_IX86) || defined(_M_X64))
192 char str
[sizeof(int[4])];
195 (__cpuid
)(cpuinf
[0].regs
, 0);
196 if(cpuinf
[0].regs
[0] == 0)
197 ERR("Failed to get CPUID\n");
200 unsigned int maxfunc
= cpuinf
[0].regs
[0];
201 unsigned int maxextfunc
;
203 (__cpuid
)(cpuinf
[0].regs
, 0x80000000);
204 maxextfunc
= cpuinf
[0].regs
[0];
206 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc
, maxextfunc
);
208 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf
[0].str
+4, cpuinf
[0].str
+12, cpuinf
[0].str
+8);
209 if(maxextfunc
>= 0x80000004)
211 (__cpuid
)(cpuinf
[0].regs
, 0x80000002);
212 (__cpuid
)(cpuinf
[1].regs
, 0x80000003);
213 (__cpuid
)(cpuinf
[2].regs
, 0x80000004);
214 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf
[0].str
, cpuinf
[1].str
, cpuinf
[2].str
);
219 (__cpuid
)(cpuinf
[0].regs
, 1);
220 if((cpuinf
[0].regs
[3]&(1<<25)))
223 if((cpuinf
[0].regs
[3]&(1<<26)))
225 caps
|= CPU_CAP_SSE2
;
226 if((cpuinf
[0].regs
[2]&(1<<0)))
228 caps
|= CPU_CAP_SSE3
;
229 if((cpuinf
[0].regs
[2]&(1<<19)))
230 caps
|= CPU_CAP_SSE4_1
;
237 /* Assume support for whatever's supported if we can't check for it */
238 #if defined(HAVE_SSE4_1)
239 #warning "Assuming SSE 4.1 run-time support!"
240 caps
|= CPU_CAP_SSE
| CPU_CAP_SSE2
| CPU_CAP_SSE3
| CPU_CAP_SSE4_1
;
241 #elif defined(HAVE_SSE3)
242 #warning "Assuming SSE 3 run-time support!"
243 caps
|= CPU_CAP_SSE
| CPU_CAP_SSE2
| CPU_CAP_SSE3
;
244 #elif defined(HAVE_SSE2)
245 #warning "Assuming SSE 2 run-time support!"
246 caps
|= CPU_CAP_SSE
| CPU_CAP_SSE2
;
247 #elif defined(HAVE_SSE)
248 #warning "Assuming SSE run-time support!"
253 FILE *file
= fopen("/proc/cpuinfo", "rt");
255 ERR("Failed to open /proc/cpuinfo, cannot check for NEON support\n");
259 while(fgets(buf
, sizeof(buf
), file
) != NULL
)
264 if(strncmp(buf
, "Features\t:", 10) != 0)
268 while(len
> 0 && isspace(buf
[len
-1]))
271 TRACE("Got features string:%s\n", buf
+10);
274 while((str
=strstr(str
, "neon")) != NULL
)
276 if(isspace(*(str
-1)) && (str
[4] == 0 || isspace(str
[4])))
278 caps
|= CPU_CAP_NEON
;
291 TRACE("Extensions:%s%s%s%s%s%s\n",
292 ((capfilter
&CPU_CAP_SSE
) ? ((caps
&CPU_CAP_SSE
) ? " +SSE" : " -SSE") : ""),
293 ((capfilter
&CPU_CAP_SSE2
) ? ((caps
&CPU_CAP_SSE2
) ? " +SSE2" : " -SSE2") : ""),
294 ((capfilter
&CPU_CAP_SSE3
) ? ((caps
&CPU_CAP_SSE3
) ? " +SSE3" : " -SSE3") : ""),
295 ((capfilter
&CPU_CAP_SSE4_1
) ? ((caps
&CPU_CAP_SSE4_1
) ? " +SSE4.1" : " -SSE4.1") : ""),
296 ((capfilter
&CPU_CAP_NEON
) ? ((caps
&CPU_CAP_NEON
) ? " +NEON" : " -NEON") : ""),
297 ((!capfilter
) ? " -none-" : "")
299 CPUCapFlags
= caps
& capfilter
;
303 void SetMixerFPUMode(FPUCtl
*ctl
)
306 fegetenv(&ctl
->flt_env
);
308 /* HACK: A nasty bug in MinGW-W64 causes fegetenv and fesetenv to not save
309 * and restore the FPU rounding mode, so we have to do it manually. Don't
310 * know if this also applies to MSVC.
312 ctl
->round_mode
= fegetround();
314 #if defined(__GNUC__) && defined(HAVE_SSE)
315 /* FIXME: Some fegetenv implementations can get the SSE environment too?
316 * How to tell when it does? */
317 if((CPUCapFlags
&CPU_CAP_SSE
))
318 __asm__
__volatile__("stmxcsr %0" : "=m" (*&ctl
->sse_state
));
322 fesetround(FE_TOWARDZERO
);
324 #if defined(__GNUC__) && defined(HAVE_SSE)
325 if((CPUCapFlags
&CPU_CAP_SSE
))
327 int sseState
= ctl
->sse_state
;
328 sseState
|= 0x6000; /* set round-to-zero */
329 sseState
|= 0x8000; /* set flush-to-zero */
330 if((CPUCapFlags
&CPU_CAP_SSE2
))
331 sseState
|= 0x0040; /* set denormals-are-zero */
332 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&sseState
));
336 #elif defined(HAVE___CONTROL87_2)
339 __control87_2(0, 0, &ctl
->state
, NULL
);
340 __control87_2(_RC_CHOP
, _MCW_RC
, &mode
, NULL
);
342 if((CPUCapFlags
&CPU_CAP_SSE
))
344 __control87_2(0, 0, NULL
, &ctl
->sse_state
);
345 __control87_2(_RC_CHOP
|_DN_FLUSH
, _MCW_RC
|_MCW_DN
, NULL
, &mode
);
349 #elif defined(HAVE__CONTROLFP)
351 ctl
->state
= _controlfp(0, 0);
352 (void)_controlfp(_RC_CHOP
, _MCW_RC
);
356 void RestoreFPUMode(const FPUCtl
*ctl
)
359 fesetenv(&ctl
->flt_env
);
361 fesetround(ctl
->round_mode
);
363 #if defined(__GNUC__) && defined(HAVE_SSE)
364 if((CPUCapFlags
&CPU_CAP_SSE
))
365 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&ctl
->sse_state
));
368 #elif defined(HAVE___CONTROL87_2)
371 __control87_2(ctl
->state
, _MCW_RC
, &mode
, NULL
);
373 if((CPUCapFlags
&CPU_CAP_SSE
))
374 __control87_2(ctl
->sse_state
, _MCW_RC
|_MCW_DN
, NULL
, &mode
);
377 #elif defined(HAVE__CONTROLFP)
379 _controlfp(ctl
->state
, _MCW_RC
);
384 static int StringSortCompare(const void *str1
, const void *str2
)
386 return alstr_cmp(*(const_al_string
*)str1
, *(const_al_string
*)str2
);
391 static WCHAR
*strrchrW(WCHAR
*str
, WCHAR ch
)
403 void GetProcBinary(al_string
*path
, al_string
*fname
)
405 WCHAR
*pathname
, *sep
;
410 pathname
= malloc(pathlen
* sizeof(pathname
[0]));
411 while(pathlen
> 0 && (len
=GetModuleFileNameW(NULL
, pathname
, pathlen
)) == pathlen
)
415 pathname
= malloc(pathlen
* sizeof(pathname
[0]));
420 ERR("Failed to get process name: error %lu\n", GetLastError());
425 if((sep
=strrchrW(pathname
, '\\')) != NULL
)
427 WCHAR
*sep2
= strrchrW(sep
+1, '/');
431 sep
= strrchrW(pathname
, '/');
435 if(path
) alstr_copy_wrange(path
, pathname
, sep
);
436 if(fname
) alstr_copy_wcstr(fname
, sep
+1);
440 if(path
) alstr_clear(path
);
441 if(fname
) alstr_copy_wcstr(fname
, pathname
);
446 TRACE("Got: %s, %s\n", alstr_get_cstr(*path
), alstr_get_cstr(*fname
));
447 else if(path
) TRACE("Got path: %s\n", alstr_get_cstr(*path
));
448 else if(fname
) TRACE("Got filename: %s\n", alstr_get_cstr(*fname
));
452 static WCHAR
*FromUTF8(const char *str
)
457 if((len
=MultiByteToWideChar(CP_UTF8
, 0, str
, -1, NULL
, 0)) > 0)
459 out
= calloc(sizeof(WCHAR
), len
);
460 MultiByteToWideChar(CP_UTF8
, 0, str
, -1, out
, len
);
466 void *LoadLib(const char *name
)
471 wname
= FromUTF8(name
);
473 ERR("Failed to convert UTF-8 filename: \"%s\"\n", name
);
476 hdl
= LoadLibraryW(wname
);
481 void CloseLib(void *handle
)
482 { FreeLibrary((HANDLE
)handle
); }
483 void *GetSymbol(void *handle
, const char *name
)
487 ret
= (void*)GetProcAddress((HANDLE
)handle
, name
);
489 ERR("Failed to load %s\n", name
);
493 WCHAR
*strdupW(const WCHAR
*str
)
503 ret
= calloc(sizeof(WCHAR
), len
+1);
505 memcpy(ret
, str
, sizeof(WCHAR
)*len
);
509 FILE *al_fopen(const char *fname
, const char *mode
)
511 WCHAR
*wname
=NULL
, *wmode
=NULL
;
514 wname
= FromUTF8(fname
);
515 wmode
= FromUTF8(mode
);
517 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname
);
519 ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode
);
521 file
= _wfopen(wname
, wmode
);
530 void al_print(const char *type
, const char *func
, const char *fmt
, ...)
537 vsnprintf(str
, sizeof(str
), fmt
, ap
);
540 str
[sizeof(str
)-1] = 0;
541 wstr
= FromUTF8(str
);
543 fprintf(LogFile
, "AL lib: %s %s: <UTF-8 error> %s", type
, func
, str
);
546 fprintf(LogFile
, "AL lib: %s %s: %ls", type
, func
, wstr
);
554 static inline int is_slash(int c
)
555 { return (c
== '\\' || c
== '/'); }
557 static void DirectorySearch(const char *path
, const char *ext
, vector_al_string
*results
)
559 al_string pathstr
= AL_STRING_INIT_STATIC();
560 WIN32_FIND_DATAW fdata
;
564 alstr_copy_cstr(&pathstr
, path
);
565 alstr_append_cstr(&pathstr
, "\\*");
566 alstr_append_cstr(&pathstr
, ext
);
568 TRACE("Searching %s\n", alstr_get_cstr(pathstr
));
570 wpath
= FromUTF8(alstr_get_cstr(pathstr
));
572 hdl
= FindFirstFileW(wpath
, &fdata
);
573 if(hdl
!= INVALID_HANDLE_VALUE
)
575 size_t base
= VECTOR_SIZE(*results
);
577 al_string str
= AL_STRING_INIT_STATIC();
578 alstr_copy_cstr(&str
, path
);
579 alstr_append_char(&str
, '\\');
580 alstr_append_wcstr(&str
, fdata
.cFileName
);
581 TRACE("Got result %s\n", alstr_get_cstr(str
));
582 VECTOR_PUSH_BACK(*results
, str
);
583 } while(FindNextFileW(hdl
, &fdata
));
586 if(VECTOR_SIZE(*results
) > base
)
587 qsort(VECTOR_BEGIN(*results
)+base
, VECTOR_SIZE(*results
)-base
,
588 sizeof(VECTOR_FRONT(*results
)), StringSortCompare
);
592 alstr_reset(&pathstr
);
595 vector_al_string
SearchDataFiles(const char *ext
, const char *subdir
)
597 static const int ids
[2] = { CSIDL_APPDATA
, CSIDL_COMMON_APPDATA
};
598 static RefCount search_lock
;
599 vector_al_string results
= VECTOR_INIT_STATIC();
602 while(ATOMIC_EXCHANGE_SEQ(&search_lock
, 1) == 1)
605 /* If the path is absolute, use it directly. */
606 if(isalpha(subdir
[0]) && subdir
[1] == ':' && is_slash(subdir
[2]))
608 al_string path
= AL_STRING_INIT_STATIC();
609 alstr_copy_cstr(&path
, subdir
);
610 #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
611 VECTOR_FOR_EACH(char, path
, FIX_SLASH
);
614 DirectorySearch(alstr_get_cstr(path
), ext
, &results
);
618 else if(subdir
[0] == '\\' && subdir
[1] == '\\' && subdir
[2] == '?' && subdir
[3] == '\\')
619 DirectorySearch(subdir
, ext
, &results
);
622 al_string path
= AL_STRING_INIT_STATIC();
625 /* Search the app-local directory. */
626 if((cwdbuf
=_wgetenv(L
"ALSOFT_LOCAL_PATH")) && *cwdbuf
!= '\0')
628 alstr_copy_wcstr(&path
, cwdbuf
);
629 if(is_slash(VECTOR_BACK(path
)))
631 VECTOR_POP_BACK(path
);
632 *VECTOR_END(path
) = 0;
635 else if(!(cwdbuf
=_wgetcwd(NULL
, 0)))
636 alstr_copy_cstr(&path
, ".");
639 alstr_copy_wcstr(&path
, cwdbuf
);
640 if(is_slash(VECTOR_BACK(path
)))
642 VECTOR_POP_BACK(path
);
643 *VECTOR_END(path
) = 0;
647 #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
648 VECTOR_FOR_EACH(char, path
, FIX_SLASH
);
650 DirectorySearch(alstr_get_cstr(path
), ext
, &results
);
652 /* Search the local and global data dirs. */
653 for(i
= 0;i
< COUNTOF(ids
);i
++)
655 WCHAR buffer
[MAX_PATH
];
656 if(SHGetSpecialFolderPathW(NULL
, buffer
, ids
[i
], FALSE
) != FALSE
)
658 alstr_copy_wcstr(&path
, buffer
);
659 if(!is_slash(VECTOR_BACK(path
)))
660 alstr_append_char(&path
, '\\');
661 alstr_append_cstr(&path
, subdir
);
662 #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
663 VECTOR_FOR_EACH(char, path
, FIX_SLASH
);
666 DirectorySearch(alstr_get_cstr(path
), ext
, &results
);
673 ATOMIC_STORE_SEQ(&search_lock
, 0);
679 struct FileMapping
MapFileToMem(const char *fname
)
681 struct FileMapping ret
= { NULL
, NULL
, NULL
, 0 };
682 MEMORY_BASIC_INFORMATION meminfo
;
687 wname
= FromUTF8(fname
);
689 file
= CreateFileW(wname
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
690 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
691 if(file
== INVALID_HANDLE_VALUE
)
693 ERR("Failed to open %s: %lu\n", fname
, GetLastError());
700 fmap
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
703 ERR("Failed to create map for %s: %lu\n", fname
, GetLastError());
708 ptr
= MapViewOfFile(fmap
, FILE_MAP_READ
, 0, 0, 0);
711 ERR("Failed to map %s: %lu\n", fname
, GetLastError());
717 if(VirtualQuery(ptr
, &meminfo
, sizeof(meminfo
)) != sizeof(meminfo
))
719 ERR("Failed to get map size for %s: %lu\n", fname
, GetLastError());
720 UnmapViewOfFile(ptr
);
729 ret
.len
= meminfo
.RegionSize
;
733 void UnmapFileMem(const struct FileMapping
*mapping
)
735 UnmapViewOfFile(mapping
->ptr
);
736 CloseHandle(mapping
->fmap
);
737 CloseHandle(mapping
->file
);
742 void GetProcBinary(al_string
*path
, al_string
*fname
)
744 char *pathname
= NULL
;
748 int mib
[4] = { CTL_KERN
, KERN_PROC_ARGS
, getpid() };
749 if(sysctl(mib
, 3, NULL
, &pathlen
, NULL
, 0) == -1)
750 WARN("Failed to sysctl kern.procargs.%d: %s\n", mib
[2], strerror(errno
));
753 pathname
= malloc(pathlen
+ 1);
754 sysctl(mib
, 3, (void*)pathname
, &pathlen
, NULL
, 0);
755 pathname
[pathlen
] = 0;
760 const char *selfname
;
764 pathname
= malloc(pathlen
);
766 selfname
= "/proc/self/exe";
767 len
= readlink(selfname
, pathname
, pathlen
);
768 if(len
== -1 && errno
== ENOENT
)
770 selfname
= "/proc/self/file";
771 len
= readlink(selfname
, pathname
, pathlen
);
773 if(len
== -1 && errno
== ENOENT
)
775 selfname
= "/proc/curproc/exe";
776 len
= readlink(selfname
, pathname
, pathlen
);
778 if(len
== -1 && errno
== ENOENT
)
780 selfname
= "/proc/curproc/file";
781 len
= readlink(selfname
, pathname
, pathlen
);
784 while(len
> 0 && (size_t)len
== pathlen
)
788 pathname
= malloc(pathlen
);
789 len
= readlink(selfname
, pathname
, pathlen
);
794 WARN("Failed to readlink %s: %s\n", selfname
, strerror(errno
));
801 char *sep
= strrchr(pathname
, '/');
804 if(path
) alstr_copy_range(path
, pathname
, sep
);
805 if(fname
) alstr_copy_cstr(fname
, sep
+1);
809 if(path
) alstr_clear(path
);
810 if(fname
) alstr_copy_cstr(fname
, pathname
);
815 TRACE("Got: %s, %s\n", alstr_get_cstr(*path
), alstr_get_cstr(*fname
));
816 else if(path
) TRACE("Got path: %s\n", alstr_get_cstr(*path
));
817 else if(fname
) TRACE("Got filename: %s\n", alstr_get_cstr(*fname
));
823 void *LoadLib(const char *name
)
829 handle
= dlopen(name
, RTLD_NOW
);
830 if((err
=dlerror()) != NULL
)
834 void CloseLib(void *handle
)
836 void *GetSymbol(void *handle
, const char *name
)
842 sym
= dlsym(handle
, name
);
843 if((err
=dlerror()) != NULL
)
845 WARN("Failed to load %s: %s\n", name
, err
);
851 #endif /* HAVE_DLFCN_H */
853 void al_print(const char *type
, const char *func
, const char *fmt
, ...)
858 fprintf(LogFile
, "AL lib: %s %s: ", type
, func
);
859 vfprintf(LogFile
, fmt
, ap
);
866 static void DirectorySearch(const char *path
, const char *ext
, vector_al_string
*results
)
868 size_t extlen
= strlen(ext
);
871 TRACE("Searching %s for *%s\n", path
, ext
);
875 size_t base
= VECTOR_SIZE(*results
);
876 struct dirent
*dirent
;
877 while((dirent
=readdir(dir
)) != NULL
)
881 if(strcmp(dirent
->d_name
, ".") == 0 || strcmp(dirent
->d_name
, "..") == 0)
884 len
= strlen(dirent
->d_name
);
887 if(strcasecmp(dirent
->d_name
+len
-extlen
, ext
) != 0)
891 alstr_copy_cstr(&str
, path
);
892 if(VECTOR_BACK(str
) != '/')
893 alstr_append_char(&str
, '/');
894 alstr_append_cstr(&str
, dirent
->d_name
);
895 TRACE("Got result %s\n", alstr_get_cstr(str
));
896 VECTOR_PUSH_BACK(*results
, str
);
900 if(VECTOR_SIZE(*results
) > base
)
901 qsort(VECTOR_BEGIN(*results
)+base
, VECTOR_SIZE(*results
)-base
,
902 sizeof(VECTOR_FRONT(*results
)), StringSortCompare
);
906 vector_al_string
SearchDataFiles(const char *ext
, const char *subdir
)
908 static RefCount search_lock
;
909 vector_al_string results
= VECTOR_INIT_STATIC();
911 while(ATOMIC_EXCHANGE_SEQ(&search_lock
, 1) == 1)
915 DirectorySearch(subdir
, ext
, &results
);
918 al_string path
= AL_STRING_INIT_STATIC();
919 const char *str
, *next
;
921 /* Search the app-local directory. */
922 if((str
=getenv("ALSOFT_LOCAL_PATH")) && *str
!= '\0')
923 DirectorySearch(str
, ext
, &results
);
927 char *cwdbuf
= malloc(cwdlen
);
928 while(!getcwd(cwdbuf
, cwdlen
))
935 cwdbuf
= malloc(cwdlen
);
938 DirectorySearch(".", ext
, &results
);
941 DirectorySearch(cwdbuf
, ext
, &results
);
947 // Search local data dir
948 if((str
=getenv("XDG_DATA_HOME")) != NULL
&& str
[0] != '\0')
950 alstr_copy_cstr(&path
, str
);
951 if(VECTOR_BACK(path
) != '/')
952 alstr_append_char(&path
, '/');
953 alstr_append_cstr(&path
, subdir
);
954 DirectorySearch(alstr_get_cstr(path
), ext
, &results
);
956 else if((str
=getenv("HOME")) != NULL
&& str
[0] != '\0')
958 alstr_copy_cstr(&path
, str
);
959 if(VECTOR_BACK(path
) == '/')
961 VECTOR_POP_BACK(path
);
962 *VECTOR_END(path
) = 0;
964 alstr_append_cstr(&path
, "/.local/share/");
965 alstr_append_cstr(&path
, subdir
);
966 DirectorySearch(alstr_get_cstr(path
), ext
, &results
);
969 // Search global data dirs
970 if((str
=getenv("XDG_DATA_DIRS")) == NULL
|| str
[0] == '\0')
971 str
= "/usr/local/share/:/usr/share/";
974 while((str
=next
) != NULL
&& str
[0] != '\0')
976 next
= strchr(str
, ':');
978 alstr_copy_cstr(&path
, str
);
981 alstr_copy_range(&path
, str
, next
);
984 if(!alstr_empty(path
))
986 if(VECTOR_BACK(path
) != '/')
987 alstr_append_char(&path
, '/');
988 alstr_append_cstr(&path
, subdir
);
990 DirectorySearch(alstr_get_cstr(path
), ext
, &results
);
997 ATOMIC_STORE_SEQ(&search_lock
, 0);
1003 struct FileMapping
MapFileToMem(const char *fname
)
1005 struct FileMapping ret
= { -1, NULL
, 0 };
1010 fd
= open(fname
, O_RDONLY
, 0);
1013 ERR("Failed to open %s: (%d) %s\n", fname
, errno
, strerror(errno
));
1016 if(fstat(fd
, &sbuf
) == -1)
1018 ERR("Failed to stat %s: (%d) %s\n", fname
, errno
, strerror(errno
));
1023 ptr
= mmap(NULL
, sbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1024 if(ptr
== MAP_FAILED
)
1026 ERR("Failed to map %s: (%d) %s\n", fname
, errno
, strerror(errno
));
1033 ret
.len
= sbuf
.st_size
;
1037 void UnmapFileMem(const struct FileMapping
*mapping
)
1039 munmap(mapping
->ptr
, mapping
->len
);
1046 void SetRTPriority(void)
1048 ALboolean failed
= AL_FALSE
;
1052 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
1053 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
1056 struct sched_param param
;
1057 /* Use the minimum real-time priority possible for now (on Linux this
1058 * should be 1 for SCHED_RR) */
1059 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
1060 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
1063 /* Real-time priority not available */
1064 failed
= (RTPrioLevel
>0);
1067 ERR("Failed to set priority level for thread\n");
1071 extern inline void alstr_reset(al_string
*str
);
1072 extern inline size_t alstr_length(const_al_string str
);
1073 extern inline ALboolean
alstr_empty(const_al_string str
);
1074 extern inline const al_string_char_type
*alstr_get_cstr(const_al_string str
);
1076 void alstr_clear(al_string
*str
)
1078 if(!alstr_empty(*str
))
1080 /* Reserve one more character than the total size of the string. This
1081 * is to ensure we have space to add a null terminator in the string
1082 * data so it can be used as a C-style string.
1084 VECTOR_RESIZE(*str
, 0, 1);
1085 VECTOR_ELEM(*str
, 0) = 0;
1089 static inline int alstr_compare(const al_string_char_type
*str1
, size_t str1len
,
1090 const al_string_char_type
*str2
, size_t str2len
)
1092 size_t complen
= (str1len
< str2len
) ? str1len
: str2len
;
1093 int ret
= memcmp(str1
, str2
, complen
);
1096 if(str1len
> str2len
) return 1;
1097 if(str1len
< str2len
) return -1;
1101 int alstr_cmp(const_al_string str1
, const_al_string str2
)
1103 return alstr_compare(&VECTOR_FRONT(str1
), alstr_length(str1
),
1104 &VECTOR_FRONT(str2
), alstr_length(str2
));
1106 int alstr_cmp_cstr(const_al_string str1
, const al_string_char_type
*str2
)
1108 return alstr_compare(&VECTOR_FRONT(str1
), alstr_length(str1
),
1109 str2
, strlen(str2
));
1112 void alstr_copy(al_string
*str
, const_al_string from
)
1114 size_t len
= alstr_length(from
);
1117 VECTOR_RESIZE(*str
, len
, len
+1);
1118 for(i
= 0;i
< len
;i
++)
1119 VECTOR_ELEM(*str
, i
) = VECTOR_ELEM(from
, i
);
1120 VECTOR_ELEM(*str
, i
) = 0;
1123 void alstr_copy_cstr(al_string
*str
, const al_string_char_type
*from
)
1125 size_t len
= strlen(from
);
1128 VECTOR_RESIZE(*str
, len
, len
+1);
1129 for(i
= 0;i
< len
;i
++)
1130 VECTOR_ELEM(*str
, i
) = from
[i
];
1131 VECTOR_ELEM(*str
, i
) = 0;
1134 void alstr_copy_range(al_string
*str
, const al_string_char_type
*from
, const al_string_char_type
*to
)
1136 size_t len
= to
- from
;
1139 VECTOR_RESIZE(*str
, len
, len
+1);
1140 for(i
= 0;i
< len
;i
++)
1141 VECTOR_ELEM(*str
, i
) = from
[i
];
1142 VECTOR_ELEM(*str
, i
) = 0;
1145 void alstr_append_char(al_string
*str
, const al_string_char_type c
)
1147 size_t len
= alstr_length(*str
);
1148 VECTOR_RESIZE(*str
, len
, len
+2);
1149 VECTOR_PUSH_BACK(*str
, c
);
1150 VECTOR_ELEM(*str
, len
+1) = 0;
1153 void alstr_append_cstr(al_string
*str
, const al_string_char_type
*from
)
1155 size_t len
= strlen(from
);
1158 size_t base
= alstr_length(*str
);
1161 VECTOR_RESIZE(*str
, base
+len
, base
+len
+1);
1162 for(i
= 0;i
< len
;i
++)
1163 VECTOR_ELEM(*str
, base
+i
) = from
[i
];
1164 VECTOR_ELEM(*str
, base
+i
) = 0;
1168 void alstr_append_range(al_string
*str
, const al_string_char_type
*from
, const al_string_char_type
*to
)
1170 size_t len
= to
- from
;
1173 size_t base
= alstr_length(*str
);
1176 VECTOR_RESIZE(*str
, base
+len
, base
+len
+1);
1177 for(i
= 0;i
< len
;i
++)
1178 VECTOR_ELEM(*str
, base
+i
) = from
[i
];
1179 VECTOR_ELEM(*str
, base
+i
) = 0;
1184 void alstr_copy_wcstr(al_string
*str
, const wchar_t *from
)
1187 if((len
=WideCharToMultiByte(CP_UTF8
, 0, from
, -1, NULL
, 0, NULL
, NULL
)) > 0)
1189 VECTOR_RESIZE(*str
, len
-1, len
);
1190 WideCharToMultiByte(CP_UTF8
, 0, from
, -1, &VECTOR_FRONT(*str
), len
, NULL
, NULL
);
1191 VECTOR_ELEM(*str
, len
-1) = 0;
1195 void alstr_append_wcstr(al_string
*str
, const wchar_t *from
)
1198 if((len
=WideCharToMultiByte(CP_UTF8
, 0, from
, -1, NULL
, 0, NULL
, NULL
)) > 0)
1200 size_t base
= alstr_length(*str
);
1201 VECTOR_RESIZE(*str
, base
+len
-1, base
+len
);
1202 WideCharToMultiByte(CP_UTF8
, 0, from
, -1, &VECTOR_ELEM(*str
, base
), len
, NULL
, NULL
);
1203 VECTOR_ELEM(*str
, base
+len
-1) = 0;
1207 void alstr_copy_wrange(al_string
*str
, const wchar_t *from
, const wchar_t *to
)
1210 if((len
=WideCharToMultiByte(CP_UTF8
, 0, from
, (int)(to
-from
), NULL
, 0, NULL
, NULL
)) > 0)
1212 VECTOR_RESIZE(*str
, len
, len
+1);
1213 WideCharToMultiByte(CP_UTF8
, 0, from
, (int)(to
-from
), &VECTOR_FRONT(*str
), len
+1, NULL
, NULL
);
1214 VECTOR_ELEM(*str
, len
) = 0;
1218 void alstr_append_wrange(al_string
*str
, const wchar_t *from
, const wchar_t *to
)
1221 if((len
=WideCharToMultiByte(CP_UTF8
, 0, from
, (int)(to
-from
), NULL
, 0, NULL
, NULL
)) > 0)
1223 size_t base
= alstr_length(*str
);
1224 VECTOR_RESIZE(*str
, base
+len
, base
+len
+1);
1225 WideCharToMultiByte(CP_UTF8
, 0, from
, (int)(to
-from
), &VECTOR_ELEM(*str
, base
), len
+1, NULL
, NULL
);
1226 VECTOR_ELEM(*str
, base
+len
) = 0;