Use istream for makemhr input
[openal-soft.git] / alc / helpers.cpp
blob1d5346193e5caf4064c853a9d9e596ae2bdca24a
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 <algorithm>
32 #include <cerrno>
33 #include <cstdarg>
34 #include <cstdlib>
35 #include <cstdio>
36 #include <cstring>
37 #include <mutex>
38 #include <string>
40 #ifdef HAVE_DIRENT_H
41 #include <dirent.h>
42 #endif
43 #ifdef HAVE_INTRIN_H
44 #include <intrin.h>
45 #endif
46 #ifdef HAVE_CPUID_H
47 #include <cpuid.h>
48 #endif
49 #ifdef HAVE_SSE_INTRINSICS
50 #include <xmmintrin.h>
51 #endif
52 #ifdef HAVE_SYS_SYSCONF_H
53 #include <sys/sysconf.h>
54 #endif
56 #ifdef HAVE_PROC_PIDPATH
57 #include <libproc.h>
58 #endif
60 #ifdef __FreeBSD__
61 #include <sys/types.h>
62 #include <sys/sysctl.h>
63 #endif
65 #ifndef _WIN32
66 #include <unistd.h>
67 #elif defined(_WIN32_IE)
68 #include <shlobj.h>
69 #endif
71 #include "alcmain.h"
72 #include "almalloc.h"
73 #include "alstring.h"
74 #include "compat.h"
75 #include "cpu_caps.h"
76 #include "fpu_modes.h"
77 #include "logging.h"
78 #include "strutils.h"
79 #include "vector.h"
82 #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
83 defined(_M_IX86) || defined(_M_X64))
84 using reg_type = unsigned int;
85 static inline void get_cpuid(unsigned int f, reg_type *regs)
86 { __get_cpuid(f, &regs[0], &regs[1], &regs[2], &regs[3]); }
87 #define CAN_GET_CPUID
88 #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
89 defined(_M_IX86) || defined(_M_X64))
90 using reg_type = int;
91 static inline void get_cpuid(unsigned int f, reg_type *regs)
92 { (__cpuid)(regs, f); }
93 #define CAN_GET_CPUID
94 #endif
96 int CPUCapFlags = 0;
98 void FillCPUCaps(int capfilter)
100 int caps = 0;
102 /* FIXME: We really should get this for all available CPUs in case different
103 * CPUs have different caps (is that possible on one machine?). */
104 #ifdef CAN_GET_CPUID
105 union {
106 reg_type regs[4];
107 char str[sizeof(reg_type[4])];
108 } cpuinf[3]{};
110 get_cpuid(0, cpuinf[0].regs);
111 if(cpuinf[0].regs[0] == 0)
112 ERR("Failed to get CPUID\n");
113 else
115 unsigned int maxfunc = cpuinf[0].regs[0];
116 unsigned int maxextfunc;
118 get_cpuid(0x80000000, cpuinf[0].regs);
119 maxextfunc = cpuinf[0].regs[0];
121 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
123 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
124 if(maxextfunc >= 0x80000004)
126 get_cpuid(0x80000002, cpuinf[0].regs);
127 get_cpuid(0x80000003, cpuinf[1].regs);
128 get_cpuid(0x80000004, cpuinf[2].regs);
129 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
132 if(maxfunc >= 1)
134 get_cpuid(1, cpuinf[0].regs);
135 if((cpuinf[0].regs[3]&(1<<25)))
136 caps |= CPU_CAP_SSE;
137 if((caps&CPU_CAP_SSE) && (cpuinf[0].regs[3]&(1<<26)))
138 caps |= CPU_CAP_SSE2;
139 if((caps&CPU_CAP_SSE2) && (cpuinf[0].regs[2]&(1<<0)))
140 caps |= CPU_CAP_SSE3;
141 if((caps&CPU_CAP_SSE3) && (cpuinf[0].regs[2]&(1<<19)))
142 caps |= CPU_CAP_SSE4_1;
145 #else
146 /* Assume support for whatever's supported if we can't check for it */
147 #if defined(HAVE_SSE4_1)
148 #warning "Assuming SSE 4.1 run-time support!"
149 caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
150 #elif defined(HAVE_SSE3)
151 #warning "Assuming SSE 3 run-time support!"
152 caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
153 #elif defined(HAVE_SSE2)
154 #warning "Assuming SSE 2 run-time support!"
155 caps |= CPU_CAP_SSE | CPU_CAP_SSE2;
156 #elif defined(HAVE_SSE)
157 #warning "Assuming SSE run-time support!"
158 caps |= CPU_CAP_SSE;
159 #endif
160 #endif
161 #ifdef HAVE_NEON
162 al::ifstream file{"/proc/cpuinfo"};
163 if(!file.is_open())
164 ERR("Failed to open /proc/cpuinfo, cannot check for NEON support\n");
165 else
167 std::string features;
169 auto getline = [](std::istream &f, std::string &output) -> bool
171 while(f.good() && f.peek() == '\n')
172 f.ignore();
173 return std::getline(f, output) && !output.empty();
176 while(getline(file, features))
178 if(features.compare(0, 10, "Features\t:", 10) == 0)
179 break;
181 file.close();
183 size_t extpos{9};
184 while((extpos=features.find("neon", extpos+1)) != std::string::npos)
186 if((extpos == 0 || std::isspace(features[extpos-1])) &&
187 (extpos+4 == features.length() || std::isspace(features[extpos+4])))
189 caps |= CPU_CAP_NEON;
190 break;
194 #endif
196 TRACE("Extensions:%s%s%s%s%s%s\n",
197 ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
198 ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
199 ((capfilter&CPU_CAP_SSE3) ? ((caps&CPU_CAP_SSE3) ? " +SSE3" : " -SSE3") : ""),
200 ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
201 ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +NEON" : " -NEON") : ""),
202 ((!capfilter) ? " -none-" : "")
204 CPUCapFlags = caps & capfilter;
208 FPUCtl::FPUCtl()
210 #if defined(HAVE_SSE_INTRINSICS)
211 this->sse_state = _mm_getcsr();
212 unsigned int sseState = this->sse_state;
213 sseState |= 0x8000; /* set flush-to-zero */
214 sseState |= 0x0040; /* set denormals-are-zero */
215 _mm_setcsr(sseState);
217 #elif defined(__GNUC__) && defined(HAVE_SSE)
219 if((CPUCapFlags&CPU_CAP_SSE))
221 __asm__ __volatile__("stmxcsr %0" : "=m" (*&this->sse_state));
222 unsigned int sseState = this->sse_state;
223 sseState |= 0x8000; /* set flush-to-zero */
224 if((CPUCapFlags&CPU_CAP_SSE2))
225 sseState |= 0x0040; /* set denormals-are-zero */
226 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
228 #endif
230 this->in_mode = true;
233 void FPUCtl::leave()
235 if(!this->in_mode) return;
237 #if defined(HAVE_SSE_INTRINSICS)
238 _mm_setcsr(this->sse_state);
240 #elif defined(__GNUC__) && defined(HAVE_SSE)
242 if((CPUCapFlags&CPU_CAP_SSE))
243 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&this->sse_state));
244 #endif
245 this->in_mode = false;
249 #ifdef _WIN32
251 const PathNamePair &GetProcBinary()
253 static PathNamePair ret;
254 if(!ret.fname.empty() || !ret.path.empty())
255 return ret;
257 al::vector<WCHAR> fullpath(256);
258 DWORD len;
259 while((len=GetModuleFileNameW(nullptr, fullpath.data(), static_cast<DWORD>(fullpath.size()))) == fullpath.size())
260 fullpath.resize(fullpath.size() << 1);
261 if(len == 0)
263 ERR("Failed to get process name: error %lu\n", GetLastError());
264 return ret;
267 fullpath.resize(len);
268 if(fullpath.back() != 0)
269 fullpath.push_back(0);
271 auto sep = std::find(fullpath.rbegin()+1, fullpath.rend(), '\\');
272 sep = std::find(fullpath.rbegin()+1, sep, '/');
273 if(sep != fullpath.rend())
275 *sep = 0;
276 ret.fname = wstr_to_utf8(&*sep + 1);
277 ret.path = wstr_to_utf8(fullpath.data());
279 else
280 ret.fname = wstr_to_utf8(fullpath.data());
282 TRACE("Got binary: %s, %s\n", ret.path.c_str(), ret.fname.c_str());
283 return ret;
287 void al_print(FILE *logfile, const char *fmt, ...)
289 al::vector<char> dynmsg;
290 char stcmsg[256];
291 char *str{stcmsg};
293 va_list args, args2;
294 va_start(args, fmt);
295 va_copy(args2, args);
296 int msglen{std::vsnprintf(str, sizeof(stcmsg), fmt, args)};
297 if UNLIKELY(msglen >= 0 && static_cast<size_t>(msglen) >= sizeof(stcmsg))
299 dynmsg.resize(static_cast<size_t>(msglen) + 1u);
300 str = dynmsg.data();
301 msglen = std::vsnprintf(str, dynmsg.size(), fmt, args2);
303 va_end(args2);
304 va_end(args);
306 std::wstring wstr{utf8_to_wstr(str)};
307 fprintf(logfile, "%ls", wstr.c_str());
308 fflush(logfile);
312 static inline int is_slash(int c)
313 { return (c == '\\' || c == '/'); }
315 static void DirectorySearch(const char *path, const char *ext, al::vector<std::string> *const results)
317 std::string pathstr{path};
318 pathstr += "\\*";
319 pathstr += ext;
320 TRACE("Searching %s\n", pathstr.c_str());
322 std::wstring wpath{utf8_to_wstr(pathstr.c_str())};
323 WIN32_FIND_DATAW fdata;
324 HANDLE hdl{FindFirstFileW(wpath.c_str(), &fdata)};
325 if(hdl != INVALID_HANDLE_VALUE)
327 size_t base = results->size();
328 do {
329 results->emplace_back();
330 std::string &str = results->back();
331 str = path;
332 str += '\\';
333 str += wstr_to_utf8(fdata.cFileName);
334 TRACE(" got %s\n", str.c_str());
335 } while(FindNextFileW(hdl, &fdata));
336 FindClose(hdl);
338 std::sort(results->begin()+base, results->end());
342 al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
344 static std::mutex search_lock;
345 std::lock_guard<std::mutex> _{search_lock};
347 /* If the path is absolute, use it directly. */
348 al::vector<std::string> results;
349 if(isalpha(subdir[0]) && subdir[1] == ':' && is_slash(subdir[2]))
351 std::string path{subdir};
352 std::replace(path.begin(), path.end(), '/', '\\');
353 DirectorySearch(path.c_str(), ext, &results);
354 return results;
356 if(subdir[0] == '\\' && subdir[1] == '\\' && subdir[2] == '?' && subdir[3] == '\\')
358 DirectorySearch(subdir, ext, &results);
359 return results;
362 std::string path;
364 /* Search the app-local directory. */
365 if(auto localpath = al::getenv(L"ALSOFT_LOCAL_PATH"))
367 path = wstr_to_utf8(localpath->c_str());
368 if(is_slash(path.back()))
369 path.pop_back();
371 else if(WCHAR *cwdbuf{_wgetcwd(nullptr, 0)})
373 path = wstr_to_utf8(cwdbuf);
374 if(is_slash(path.back()))
375 path.pop_back();
376 free(cwdbuf);
378 else
379 path = ".";
380 std::replace(path.begin(), path.end(), '/', '\\');
381 DirectorySearch(path.c_str(), ext, &results);
383 /* Search the local and global data dirs. */
384 static constexpr int ids[2]{ CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
385 for(int id : ids)
387 WCHAR buffer[MAX_PATH];
388 if(SHGetSpecialFolderPathW(nullptr, buffer, id, FALSE) == FALSE)
389 continue;
391 path = wstr_to_utf8(buffer);
392 if(!is_slash(path.back()))
393 path += '\\';
394 path += subdir;
395 std::replace(path.begin(), path.end(), '/', '\\');
397 DirectorySearch(path.c_str(), ext, &results);
400 return results;
403 void SetRTPriority(void)
405 bool failed = false;
406 if(RTPrioLevel > 0)
407 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
408 if(failed) ERR("Failed to set priority level for thread\n");
411 #else
413 #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
414 #include <pthread.h>
415 #include <sched.h>
416 #endif
418 const PathNamePair &GetProcBinary()
420 static PathNamePair ret;
421 if(!ret.fname.empty() || !ret.path.empty())
422 return ret;
424 al::vector<char> pathname;
425 #ifdef __FreeBSD__
426 size_t pathlen;
427 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
428 if(sysctl(mib, 4, nullptr, &pathlen, nullptr, 0) == -1)
429 WARN("Failed to sysctl kern.proc.pathname: %s\n", strerror(errno));
430 else
432 pathname.resize(pathlen + 1);
433 sysctl(mib, 4, pathname.data(), &pathlen, nullptr, 0);
434 pathname.resize(pathlen);
436 #endif
437 #ifdef HAVE_PROC_PIDPATH
438 if(pathname.empty())
440 char procpath[PROC_PIDPATHINFO_MAXSIZE]{};
441 const pid_t pid{getpid()};
442 if(proc_pidpath(pid, procpath, sizeof(procpath)) < 1)
443 ERR("proc_pidpath(%d, ...) failed: %s\n", pid, strerror(errno));
444 else
445 pathname.insert(pathname.end(), procpath, procpath+strlen(procpath));
447 #endif
448 if(pathname.empty())
450 pathname.resize(256);
452 const char *selfname{"/proc/self/exe"};
453 ssize_t len{readlink(selfname, pathname.data(), pathname.size())};
454 if(len == -1 && errno == ENOENT)
456 selfname = "/proc/self/file";
457 len = readlink(selfname, pathname.data(), pathname.size());
459 if(len == -1 && errno == ENOENT)
461 selfname = "/proc/curproc/exe";
462 len = readlink(selfname, pathname.data(), pathname.size());
464 if(len == -1 && errno == ENOENT)
466 selfname = "/proc/curproc/file";
467 len = readlink(selfname, pathname.data(), pathname.size());
470 while(len > 0 && static_cast<size_t>(len) == pathname.size())
472 pathname.resize(pathname.size() << 1);
473 len = readlink(selfname, pathname.data(), pathname.size());
475 if(len <= 0)
477 WARN("Failed to readlink %s: %s\n", selfname, strerror(errno));
478 return ret;
481 pathname.resize(static_cast<size_t>(len));
483 while(!pathname.empty() && pathname.back() == 0)
484 pathname.pop_back();
486 auto sep = std::find(pathname.crbegin(), pathname.crend(), '/');
487 if(sep != pathname.crend())
489 ret.path = std::string(pathname.cbegin(), sep.base()-1);
490 ret.fname = std::string(sep.base(), pathname.cend());
492 else
493 ret.fname = std::string(pathname.cbegin(), pathname.cend());
495 TRACE("Got binary: %s, %s\n", ret.path.c_str(), ret.fname.c_str());
496 return ret;
500 void al_print(FILE *logfile, const char *fmt, ...)
502 va_list ap;
504 va_start(ap, fmt);
505 vfprintf(logfile, fmt, ap);
506 va_end(ap);
508 fflush(logfile);
512 static void DirectorySearch(const char *path, const char *ext, al::vector<std::string> *const results)
514 TRACE("Searching %s for *%s\n", path, ext);
515 DIR *dir{opendir(path)};
516 if(dir != nullptr)
518 auto base = results->cend() - results->cbegin();
519 const size_t extlen{strlen(ext)};
521 struct dirent *dirent;
522 while((dirent=readdir(dir)) != nullptr)
524 if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
525 continue;
527 size_t len{strlen(dirent->d_name)};
528 if(len <= extlen) continue;
529 if(al::strcasecmp(dirent->d_name+len-extlen, ext) != 0)
530 continue;
532 results->emplace_back();
533 std::string &str = results->back();
534 str = path;
535 if(str.back() != '/')
536 str.push_back('/');
537 str += dirent->d_name;
538 TRACE(" got %s\n", str.c_str());
540 closedir(dir);
542 std::sort(results->begin()+base, results->end());
546 al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
548 static std::mutex search_lock;
549 std::lock_guard<std::mutex> _{search_lock};
551 al::vector<std::string> results;
552 if(subdir[0] == '/')
554 DirectorySearch(subdir, ext, &results);
555 return results;
558 /* Search the app-local directory. */
559 if(auto localpath = al::getenv("ALSOFT_LOCAL_PATH"))
560 DirectorySearch(localpath->c_str(), ext, &results);
561 else
563 al::vector<char> cwdbuf(256);
564 while(!getcwd(cwdbuf.data(), cwdbuf.size()))
566 if(errno != ERANGE)
568 cwdbuf.clear();
569 break;
571 cwdbuf.resize(cwdbuf.size() << 1);
573 if(cwdbuf.empty())
574 DirectorySearch(".", ext, &results);
575 else
577 DirectorySearch(cwdbuf.data(), ext, &results);
578 cwdbuf.clear();
582 // Search local data dir
583 if(auto datapath = al::getenv("XDG_DATA_HOME"))
585 std::string &path = *datapath;
586 if(path.back() != '/')
587 path += '/';
588 path += subdir;
589 DirectorySearch(path.c_str(), ext, &results);
591 else if(auto homepath = al::getenv("HOME"))
593 std::string &path = *homepath;
594 if(path.back() == '/')
595 path.pop_back();
596 path += "/.local/share/";
597 path += subdir;
598 DirectorySearch(path.c_str(), ext, &results);
601 // Search global data dirs
602 std::string datadirs{al::getenv("XDG_DATA_DIRS").value_or("/usr/local/share/:/usr/share/")};
604 size_t curpos{0u};
605 while(curpos < datadirs.size())
607 size_t nextpos{datadirs.find(':', curpos)};
609 std::string path{(nextpos != std::string::npos) ?
610 datadirs.substr(curpos, nextpos++ - curpos) : datadirs.substr(curpos)};
611 curpos = nextpos;
613 if(path.empty()) continue;
614 if(path.back() != '/')
615 path += '/';
616 path += subdir;
618 DirectorySearch(path.c_str(), ext, &results);
621 return results;
624 void SetRTPriority()
626 bool failed = false;
627 #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
628 if(RTPrioLevel > 0)
630 struct sched_param param;
631 /* Use the minimum real-time priority possible for now (on Linux this
632 * should be 1 for SCHED_RR) */
633 param.sched_priority = sched_get_priority_min(SCHED_RR);
634 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
636 #else
637 /* Real-time priority not available */
638 failed = (RTPrioLevel>0);
639 #endif
640 if(failed)
641 ERR("Failed to set priority level for thread\n");
644 #endif