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 #ifdef HAVE_PROC_PIDPATH
47 #include <sys/types.h>
48 #include <sys/sysctl.h>
51 #ifndef AL_NO_UID_DEFS
52 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
61 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
62 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
64 DEFINE_GUID(IID_IDirectSoundNotify
, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
66 DEFINE_GUID(CLSID_MMDeviceEnumerator
, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
67 DEFINE_GUID(IID_IMMDeviceEnumerator
, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
68 DEFINE_GUID(IID_IAudioClient
, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
69 DEFINE_GUID(IID_IAudioRenderClient
, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
70 DEFINE_GUID(IID_IAudioCaptureClient
, 0xc8adbd64, 0xe71e, 0x48a0, 0xa4,0xde, 0x18,0x5c,0x39,0x5c,0xd3,0x17);
74 #include <devpropdef.h>
75 #include <propkeydef.h>
76 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName
, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
77 DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_FormFactor
, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 0);
78 DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_GUID
, 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x23,0xe0, 0xc0,0xff,0xee,0x7f,0x0e, 4 );
81 #endif /* AL_NO_UID_DEFS */
92 #ifdef HAVE_SYS_SYSCONF_H
93 #include <sys/sysconf.h>
103 #include <sys/types.h>
104 #include <sys/stat.h>
105 #include <sys/mman.h>
108 #elif defined(_WIN32_IE)
119 #include "cpu_caps.h"
120 #include "fpu_modes.h"
127 #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
128 defined(_M_IX86) || defined(_M_X64))
129 typedef unsigned int reg_type
;
130 static inline void get_cpuid(int f
, reg_type
*regs
)
131 { __get_cpuid(f
, ®s
[0], ®s
[1], ®s
[2], ®s
[3]); }
132 #define CAN_GET_CPUID
133 #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
134 defined(_M_IX86) || defined(_M_X64))
135 typedef int reg_type
;
136 static inline void get_cpuid(int f
, reg_type
*regs
)
137 { (__cpuid
)(regs
, f
); }
138 #define CAN_GET_CPUID
143 void FillCPUCaps(int capfilter
)
147 /* FIXME: We really should get this for all available CPUs in case different
148 * CPUs have different caps (is that possible on one machine?). */
152 char str
[sizeof(reg_type
[4])];
153 } cpuinf
[3] = {{ { 0, 0, 0, 0 } }};
155 get_cpuid(0, cpuinf
[0].regs
);
156 if(cpuinf
[0].regs
[0] == 0)
157 ERR("Failed to get CPUID\n");
160 unsigned int maxfunc
= cpuinf
[0].regs
[0];
161 unsigned int maxextfunc
;
163 get_cpuid(0x80000000, cpuinf
[0].regs
);
164 maxextfunc
= cpuinf
[0].regs
[0];
166 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc
, maxextfunc
);
168 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf
[0].str
+4, cpuinf
[0].str
+12, cpuinf
[0].str
+8);
169 if(maxextfunc
>= 0x80000004)
171 get_cpuid(0x80000002, cpuinf
[0].regs
);
172 get_cpuid(0x80000003, cpuinf
[1].regs
);
173 get_cpuid(0x80000004, cpuinf
[2].regs
);
174 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf
[0].str
, cpuinf
[1].str
, cpuinf
[2].str
);
179 get_cpuid(1, cpuinf
[0].regs
);
180 if((cpuinf
[0].regs
[3]&(1<<25)))
182 if((caps
&CPU_CAP_SSE
) && (cpuinf
[0].regs
[3]&(1<<26)))
183 caps
|= CPU_CAP_SSE2
;
184 if((caps
&CPU_CAP_SSE2
) && (cpuinf
[0].regs
[2]&(1<<0)))
185 caps
|= CPU_CAP_SSE3
;
186 if((caps
&CPU_CAP_SSE3
) && (cpuinf
[0].regs
[2]&(1<<19)))
187 caps
|= CPU_CAP_SSE4_1
;
191 /* Assume support for whatever's supported if we can't check for it */
192 #if defined(HAVE_SSE4_1)
193 #warning "Assuming SSE 4.1 run-time support!"
194 caps
|= CPU_CAP_SSE
| CPU_CAP_SSE2
| CPU_CAP_SSE3
| CPU_CAP_SSE4_1
;
195 #elif defined(HAVE_SSE3)
196 #warning "Assuming SSE 3 run-time support!"
197 caps
|= CPU_CAP_SSE
| CPU_CAP_SSE2
| CPU_CAP_SSE3
;
198 #elif defined(HAVE_SSE2)
199 #warning "Assuming SSE 2 run-time support!"
200 caps
|= CPU_CAP_SSE
| CPU_CAP_SSE2
;
201 #elif defined(HAVE_SSE)
202 #warning "Assuming SSE run-time support!"
207 FILE *file
= fopen("/proc/cpuinfo", "rt");
209 ERR("Failed to open /proc/cpuinfo, cannot check for NEON support\n");
212 std::string features
;
215 while(fgets(buf
, sizeof(buf
), file
) != nullptr)
217 if(strncmp(buf
, "Features\t:", 10) != 0)
221 while(features
.back() != '\n')
223 if(fgets(buf
, sizeof(buf
), file
) == nullptr)
232 if(!features
.empty())
234 const char *str
= features
.c_str();
235 while(isspace(str
[0])) ++str
;
237 TRACE("Got features string:%s\n", str
);
238 while((str
=strstr(str
, "neon")) != nullptr)
240 if(isspace(*(str
-1)) && (str
[4] == 0 || isspace(str
[4])))
242 caps
|= CPU_CAP_NEON
;
251 TRACE("Extensions:%s%s%s%s%s%s\n",
252 ((capfilter
&CPU_CAP_SSE
) ? ((caps
&CPU_CAP_SSE
) ? " +SSE" : " -SSE") : ""),
253 ((capfilter
&CPU_CAP_SSE2
) ? ((caps
&CPU_CAP_SSE2
) ? " +SSE2" : " -SSE2") : ""),
254 ((capfilter
&CPU_CAP_SSE3
) ? ((caps
&CPU_CAP_SSE3
) ? " +SSE3" : " -SSE3") : ""),
255 ((capfilter
&CPU_CAP_SSE4_1
) ? ((caps
&CPU_CAP_SSE4_1
) ? " +SSE4.1" : " -SSE4.1") : ""),
256 ((capfilter
&CPU_CAP_NEON
) ? ((caps
&CPU_CAP_NEON
) ? " +NEON" : " -NEON") : ""),
257 ((!capfilter
) ? " -none-" : "")
259 CPUCapFlags
= caps
& capfilter
;
263 void SetMixerFPUMode(FPUCtl
*ctl
)
265 #if defined(__GNUC__) && defined(HAVE_SSE)
266 if((CPUCapFlags
&CPU_CAP_SSE
))
268 __asm__
__volatile__("stmxcsr %0" : "=m" (*&ctl
->sse_state
));
269 unsigned int sseState
= ctl
->sse_state
;
270 sseState
|= 0x8000; /* set flush-to-zero */
271 if((CPUCapFlags
&CPU_CAP_SSE2
))
272 sseState
|= 0x0040; /* set denormals-are-zero */
273 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&sseState
));
276 #elif defined(HAVE___CONTROL87_2)
278 __control87_2(0, 0, &ctl
->state
, &ctl
->sse_state
);
279 _control87(_DN_FLUSH
, _MCW_DN
);
281 #elif defined(HAVE__CONTROLFP)
283 ctl
->state
= _controlfp(0, 0);
284 _controlfp(_DN_FLUSH
, _MCW_DN
);
288 void RestoreFPUMode(const FPUCtl
*ctl
)
290 #if defined(__GNUC__) && defined(HAVE_SSE)
291 if((CPUCapFlags
&CPU_CAP_SSE
))
292 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&ctl
->sse_state
));
294 #elif defined(HAVE___CONTROL87_2)
297 __control87_2(ctl
->state
, _MCW_DN
, &mode
, nullptr);
298 __control87_2(ctl
->sse_state
, _MCW_DN
, nullptr, &mode
);
300 #elif defined(HAVE__CONTROLFP)
302 _controlfp(ctl
->state
, _MCW_DN
);
309 PathNamePair
GetProcBinary()
313 std::vector
<WCHAR
> fullpath(256);
315 while((len
=GetModuleFileNameW(nullptr, fullpath
.data(), fullpath
.size())) == fullpath
.size())
316 fullpath
.resize(fullpath
.size() << 1);
319 ERR("Failed to get process name: error %lu\n", GetLastError());
323 fullpath
.resize(len
);
324 if(fullpath
.back() != 0)
325 fullpath
.push_back(0);
327 auto sep
= std::find(fullpath
.rbegin()+1, fullpath
.rend(), '\\');
328 sep
= std::find(fullpath
.rbegin()+1, sep
, '/');
329 if(sep
!= fullpath
.rend())
332 ret
.fname
= wstr_to_utf8(&*sep
+ 1);
333 ret
.path
= wstr_to_utf8(fullpath
.data());
336 ret
.fname
= wstr_to_utf8(fullpath
.data());
338 TRACE("Got: %s, %s\n", ret
.path
.c_str(), ret
.fname
.c_str());
343 void *LoadLib(const char *name
)
345 std::wstring wname
{utf8_to_wstr(name
)};
346 return LoadLibraryW(wname
.c_str());
348 void CloseLib(void *handle
)
349 { FreeLibrary(static_cast<HMODULE
>(handle
)); }
350 void *GetSymbol(void *handle
, const char *name
)
352 void *ret
{reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE
>(handle
), name
))};
353 if(!ret
) ERR("Failed to load %s\n", name
);
358 void al_print(const char *type
, const char *func
, const char *fmt
, ...)
364 vsnprintf(str
, sizeof(str
), fmt
, ap
);
366 str
[sizeof(str
)-1] = 0;
368 std::wstring wstr
{utf8_to_wstr(str
)};
369 fprintf(LogFile
, "AL lib: %s %s: %ls", type
, func
, wstr
.c_str());
374 static inline int is_slash(int c
)
375 { return (c
== '\\' || c
== '/'); }
377 static void DirectorySearch(const char *path
, const char *ext
, std::vector
<std::string
> *const results
)
379 std::string pathstr
{path
};
382 TRACE("Searching %s\n", pathstr
.c_str());
384 std::wstring wpath
{utf8_to_wstr(pathstr
.c_str())};
385 WIN32_FIND_DATAW fdata
;
386 HANDLE hdl
{FindFirstFileW(wpath
.c_str(), &fdata
)};
387 if(hdl
!= INVALID_HANDLE_VALUE
)
389 size_t base
= results
->size();
391 results
->emplace_back();
392 std::string
&str
= results
->back();
395 str
+= wstr_to_utf8(fdata
.cFileName
);
396 TRACE("Got result %s\n", str
.c_str());
397 } while(FindNextFileW(hdl
, &fdata
));
400 std::sort(results
->begin()+base
, results
->end());
404 std::vector
<std::string
> SearchDataFiles(const char *ext
, const char *subdir
)
406 static std::mutex search_lock
;
407 std::lock_guard
<std::mutex
> _
{search_lock
};
409 /* If the path is absolute, use it directly. */
410 std::vector
<std::string
> results
;
411 if(isalpha(subdir
[0]) && subdir
[1] == ':' && is_slash(subdir
[2]))
413 std::string path
{subdir
};
414 std::replace(path
.begin(), path
.end(), '/', '\\');
415 DirectorySearch(path
.c_str(), ext
, &results
);
418 if(subdir
[0] == '\\' && subdir
[1] == '\\' && subdir
[2] == '?' && subdir
[3] == '\\')
420 DirectorySearch(subdir
, ext
, &results
);
426 /* Search the app-local directory. */
427 WCHAR
*cwdbuf
{_wgetenv(L
"ALSOFT_LOCAL_PATH")};
428 if(cwdbuf
&& *cwdbuf
!= '\0')
430 path
= wstr_to_utf8(cwdbuf
);
431 if(is_slash(path
.back()))
434 else if(!(cwdbuf
=_wgetcwd(nullptr, 0)))
438 path
= wstr_to_utf8(cwdbuf
);
439 if(is_slash(path
.back()))
443 std::replace(path
.begin(), path
.end(), '/', '\\');
444 DirectorySearch(path
.c_str(), ext
, &results
);
446 /* Search the local and global data dirs. */
447 static constexpr int ids
[2]{ CSIDL_APPDATA
, CSIDL_COMMON_APPDATA
};
450 WCHAR buffer
[MAX_PATH
];
451 if(SHGetSpecialFolderPathW(nullptr, buffer
, id
, FALSE
) == FALSE
)
454 path
= wstr_to_utf8(buffer
);
455 if(!is_slash(path
.back()))
458 std::replace(path
.begin(), path
.end(), '/', '\\');
460 DirectorySearch(path
.c_str(), ext
, &results
);
466 void SetRTPriority(void)
470 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
471 if(failed
) ERR("Failed to set priority level for thread\n");
476 PathNamePair
GetProcBinary()
479 std::vector
<char> pathname
;
483 int mib
[4] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1 };
484 if(sysctl(mib
, 4, nullptr, &pathlen
, nullptr, 0) == -1)
485 WARN("Failed to sysctl kern.proc.pathname: %s\n", strerror(errno
));
488 pathname
.resize(pathlen
+ 1);
489 sysctl(mib
, 4, pathname
.data(), &pathlen
, nullptr, 0);
490 pathname
.resize(pathlen
);
493 #ifdef HAVE_PROC_PIDPATH
496 char procpath
[PROC_PIDPATHINFO_MAXSIZE
]{};
497 const pid_t pid
{getpid()};
498 if(proc_pidpath(pid
, procpath
, sizeof(procpath
)) < 1)
499 ERR("proc_pidpath(%d, ...) failed: %s\n", pid
, strerror(errno
));
501 pathname
.insert(pathname
.end(), procpath
, procpath
+strlen(procpath
));
506 pathname
.resize(256);
508 const char *selfname
{"/proc/self/exe"};
509 ssize_t len
{readlink(selfname
, pathname
.data(), pathname
.size())};
510 if(len
== -1 && errno
== ENOENT
)
512 selfname
= "/proc/self/file";
513 len
= readlink(selfname
, pathname
.data(), pathname
.size());
515 if(len
== -1 && errno
== ENOENT
)
517 selfname
= "/proc/curproc/exe";
518 len
= readlink(selfname
, pathname
.data(), pathname
.size());
520 if(len
== -1 && errno
== ENOENT
)
522 selfname
= "/proc/curproc/file";
523 len
= readlink(selfname
, pathname
.data(), pathname
.size());
526 while(len
> 0 && (size_t)len
== pathname
.size())
528 pathname
.resize(pathname
.size() << 1);
529 len
= readlink(selfname
, pathname
.data(), pathname
.size());
533 WARN("Failed to readlink %s: %s\n", selfname
, strerror(errno
));
537 pathname
.resize(len
);
539 while(!pathname
.empty() && pathname
.back() == 0)
542 auto sep
= std::find(pathname
.crbegin(), pathname
.crend(), '/');
543 if(sep
!= pathname
.crend())
545 ret
.path
= std::string(pathname
.cbegin(), sep
.base()-1);
546 ret
.fname
= std::string(sep
.base(), pathname
.cend());
549 ret
.fname
= std::string(pathname
.cbegin(), pathname
.cend());
551 TRACE("Got: %s, %s\n", ret
.path
.c_str(), ret
.fname
.c_str());
558 void *LoadLib(const char *name
)
561 void *handle
{dlopen(name
, RTLD_NOW
)};
562 const char *err
{dlerror()};
563 if(err
) handle
= nullptr;
566 void CloseLib(void *handle
)
568 void *GetSymbol(void *handle
, const char *name
)
571 void *sym
{dlsym(handle
, name
)};
572 const char *err
{dlerror()};
575 WARN("Failed to load %s: %s\n", name
, err
);
581 #endif /* HAVE_DLFCN_H */
583 void al_print(const char *type
, const char *func
, const char *fmt
, ...)
588 fprintf(LogFile
, "AL lib: %s %s: ", type
, func
);
589 vfprintf(LogFile
, fmt
, ap
);
596 static void DirectorySearch(const char *path
, const char *ext
, std::vector
<std::string
> *const results
)
598 TRACE("Searching %s for *%s\n", path
, ext
);
599 DIR *dir
{opendir(path
)};
602 const size_t extlen
= strlen(ext
);
603 size_t base
= results
->size();
605 struct dirent
*dirent
;
606 while((dirent
=readdir(dir
)) != nullptr)
608 if(strcmp(dirent
->d_name
, ".") == 0 || strcmp(dirent
->d_name
, "..") == 0)
611 size_t len
{strlen(dirent
->d_name
)};
612 if(len
<= extlen
) continue;
613 if(strcasecmp(dirent
->d_name
+len
-extlen
, ext
) != 0)
616 results
->emplace_back();
617 std::string
&str
= results
->back();
619 if(str
.back() != '/')
621 str
+= dirent
->d_name
;
622 TRACE("Got result %s\n", str
.c_str());
626 std::sort(results
->begin()+base
, results
->end());
630 std::vector
<std::string
> SearchDataFiles(const char *ext
, const char *subdir
)
632 static std::mutex search_lock
;
633 std::lock_guard
<std::mutex
> _
{search_lock
};
635 std::vector
<std::string
> results
;
638 DirectorySearch(subdir
, ext
, &results
);
642 /* Search the app-local directory. */
643 const char *str
{getenv("ALSOFT_LOCAL_PATH")};
644 if(str
&& *str
!= '\0')
645 DirectorySearch(str
, ext
, &results
);
648 std::vector
<char> cwdbuf(256);
649 while(!getcwd(cwdbuf
.data(), cwdbuf
.size()))
656 cwdbuf
.resize(cwdbuf
.size() << 1);
659 DirectorySearch(".", ext
, &results
);
662 DirectorySearch(cwdbuf
.data(), ext
, &results
);
667 // Search local data dir
668 if((str
=getenv("XDG_DATA_HOME")) != nullptr && str
[0] != '\0')
670 std::string path
{str
};
671 if(path
.back() != '/')
674 DirectorySearch(path
.c_str(), ext
, &results
);
676 else if((str
=getenv("HOME")) != nullptr && str
[0] != '\0')
678 std::string path
{str
};
679 if(path
.back() == '/')
681 path
+= "/.local/share/";
683 DirectorySearch(path
.c_str(), ext
, &results
);
686 // Search global data dirs
687 if((str
=getenv("XDG_DATA_DIRS")) == nullptr || str
[0] == '\0')
688 str
= "/usr/local/share/:/usr/share/";
690 const char *next
{str
};
691 while((str
=next
) != nullptr && str
[0] != '\0')
693 next
= strchr(str
, ':');
695 std::string path
= (next
? std::string(str
, next
++) : std::string(str
));
696 if(path
.empty()) continue;
698 if(path
.back() != '/')
702 DirectorySearch(path
.c_str(), ext
, &results
);
708 void SetRTPriority(void)
711 #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
714 struct sched_param param
;
715 /* Use the minimum real-time priority possible for now (on Linux this
716 * should be 1 for SCHED_RR) */
717 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
718 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
721 /* Real-time priority not available */
722 failed
= (RTPrioLevel
>0);
725 ERR("Failed to set priority level for thread\n");