From d2f587ee23f3e538d6d2744b9acae2e15e047db8 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 22 Apr 2021 03:14:31 -0700 Subject: Move helpers.cpp to core --- alc/alc.cpp | 2 +- alc/alconfig.cpp | 2 +- alc/backends/alsa.cpp | 2 +- alc/backends/dsound.cpp | 2 +- alc/backends/jack.cpp | 2 +- alc/backends/null.cpp | 2 +- alc/backends/opensl.cpp | 2 +- alc/backends/oss.cpp | 2 +- alc/backends/pulseaudio.cpp | 2 +- alc/backends/sndio.cpp | 2 +- alc/backends/solaris.cpp | 2 +- alc/backends/wasapi.cpp | 2 +- alc/backends/wave.cpp | 2 +- alc/backends/winmm.cpp | 2 +- alc/compat.h | 18 -- alc/helpers.cpp | 528 -------------------------------------------- alc/hrtf.cpp | 2 +- 17 files changed, 15 insertions(+), 561 deletions(-) delete mode 100644 alc/compat.h delete mode 100644 alc/helpers.cpp (limited to 'alc') diff --git a/alc/alc.cpp b/alc/alc.cpp index b79ea9c8..c4e2d61c 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -79,12 +79,12 @@ #include "async_event.h" #include "atomic.h" #include "bformatdec.h" -#include "compat.h" #include "core/ambidefs.h" #include "core/bs2b.h" #include "core/cpu_caps.h" #include "core/devformat.h" #include "core/except.h" +#include "core/helpers.h" #include "core/mastering.h" #include "core/filters/nfc.h" #include "core/filters/splitter.h" diff --git a/alc/alconfig.cpp b/alc/alconfig.cpp index 634679aa..5e00c054 100644 --- a/alc/alconfig.cpp +++ b/alc/alconfig.cpp @@ -40,7 +40,7 @@ #include "alfstream.h" #include "alstring.h" -#include "compat.h" +#include "core/helpers.h" #include "core/logging.h" #include "strutils.h" #include "vector.h" diff --git a/alc/backends/alsa.cpp b/alc/backends/alsa.cpp index fe19be04..1153b99a 100644 --- a/alc/backends/alsa.cpp +++ b/alc/backends/alsa.cpp @@ -42,7 +42,7 @@ #include "alnumeric.h" #include "aloptional.h" #include "alu.h" -#include "compat.h" +#include "core/helpers.h" #include "core/logging.h" #include "dynload.h" #include "ringbuffer.h" diff --git a/alc/backends/dsound.cpp b/alc/backends/dsound.cpp index 72f9c47a..3c6c5615 100644 --- a/alc/backends/dsound.cpp +++ b/alc/backends/dsound.cpp @@ -46,8 +46,8 @@ #include "alcmain.h" #include "alu.h" -#include "compat.h" #include "comptr.h" +#include "core/helpers.h" #include "core/logging.h" #include "dynload.h" #include "ringbuffer.h" diff --git a/alc/backends/jack.cpp b/alc/backends/jack.cpp index cf52e7a4..fe86f08c 100644 --- a/alc/backends/jack.cpp +++ b/alc/backends/jack.cpp @@ -34,7 +34,7 @@ #include "alcmain.h" #include "alu.h" #include "alconfig.h" -#include "compat.h" +#include "core/helpers.h" #include "core/logging.h" #include "dynload.h" #include "ringbuffer.h" diff --git a/alc/backends/null.cpp b/alc/backends/null.cpp index bf267163..d3dfc3a4 100644 --- a/alc/backends/null.cpp +++ b/alc/backends/null.cpp @@ -33,7 +33,7 @@ #include "alcmain.h" #include "almalloc.h" #include "alu.h" -#include "compat.h" +#include "core/helpers.h" #include "threads.h" diff --git a/alc/backends/opensl.cpp b/alc/backends/opensl.cpp index cae17ce1..8e5c2783 100644 --- a/alc/backends/opensl.cpp +++ b/alc/backends/opensl.cpp @@ -35,7 +35,7 @@ #include "albit.h" #include "alcmain.h" #include "alu.h" -#include "compat.h" +#include "core/helpers.h" #include "core/logging.h" #include "opthelpers.h" #include "ringbuffer.h" diff --git a/alc/backends/oss.cpp b/alc/backends/oss.cpp index 3db80961..22289b74 100644 --- a/alc/backends/oss.cpp +++ b/alc/backends/oss.cpp @@ -48,7 +48,7 @@ #include "alnumeric.h" #include "aloptional.h" #include "alu.h" -#include "compat.h" +#include "core/helpers.h" #include "core/logging.h" #include "ringbuffer.h" #include "threads.h" diff --git a/alc/backends/pulseaudio.cpp b/alc/backends/pulseaudio.cpp index 0011c1c7..bf3fc18e 100644 --- a/alc/backends/pulseaudio.cpp +++ b/alc/backends/pulseaudio.cpp @@ -38,7 +38,7 @@ #include "alcmain.h" #include "alu.h" #include "alconfig.h" -#include "compat.h" +#include "core/helpers.h" #include "core/logging.h" #include "dynload.h" #include "strutils.h" diff --git a/alc/backends/sndio.cpp b/alc/backends/sndio.cpp index f5dbdcb2..35b7e6c8 100644 --- a/alc/backends/sndio.cpp +++ b/alc/backends/sndio.cpp @@ -31,7 +31,7 @@ #include "alcmain.h" #include "alu.h" -#include "compat.h" +#include "core/helpers.h" #include "core/logging.h" #include "ringbuffer.h" #include "threads.h" diff --git a/alc/backends/solaris.cpp b/alc/backends/solaris.cpp index 68ab814c..7739cba7 100644 --- a/alc/backends/solaris.cpp +++ b/alc/backends/solaris.cpp @@ -43,7 +43,7 @@ #include "albyte.h" #include "alu.h" #include "alconfig.h" -#include "compat.h" +#include "core/helpers.h" #include "core/logging.h" #include "threads.h" #include "vector.h" diff --git a/alc/backends/wasapi.cpp b/alc/backends/wasapi.cpp index 6e54fa48..4df64e73 100644 --- a/alc/backends/wasapi.cpp +++ b/alc/backends/wasapi.cpp @@ -58,9 +58,9 @@ #include "albit.h" #include "alcmain.h" #include "alu.h" -#include "compat.h" #include "comptr.h" #include "converter.h" +#include "core/helpers.h" #include "core/logging.h" #include "ringbuffer.h" #include "strutils.h" diff --git a/alc/backends/wave.cpp b/alc/backends/wave.cpp index e80fb3ae..00ff6ec6 100644 --- a/alc/backends/wave.cpp +++ b/alc/backends/wave.cpp @@ -40,7 +40,7 @@ #include "almalloc.h" #include "alnumeric.h" #include "alu.h" -#include "compat.h" +#include "core/helpers.h" #include "core/logging.h" #include "opthelpers.h" #include "strutils.h" diff --git a/alc/backends/winmm.cpp b/alc/backends/winmm.cpp index 14fda45b..693c4664 100644 --- a/alc/backends/winmm.cpp +++ b/alc/backends/winmm.cpp @@ -40,7 +40,7 @@ #include "alcmain.h" #include "alu.h" -#include "compat.h" +#include "core/helpers.h" #include "core/logging.h" #include "ringbuffer.h" #include "strutils.h" diff --git a/alc/compat.h b/alc/compat.h deleted file mode 100644 index d858e5c9..00000000 --- a/alc/compat.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef AL_COMPAT_H -#define AL_COMPAT_H - -#include - -#include "vector.h" - - -struct PathNamePair { std::string path, fname; }; -const PathNamePair &GetProcBinary(void); - -extern int RTPrioLevel; -extern bool AllowRTTimeLimit; -void SetRTPriority(void); - -al::vector SearchDataFiles(const char *match, const char *subdir); - -#endif /* AL_COMPAT_H */ diff --git a/alc/helpers.cpp b/alc/helpers.cpp deleted file mode 100644 index ae9ba4ba..00000000 --- a/alc/helpers.cpp +++ /dev/null @@ -1,528 +0,0 @@ -/** - * OpenAL cross platform audio library - * Copyright (C) 2011 by authors. - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * Or go to http://www.gnu.org/copyleft/lgpl.html - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "alcmain.h" -#include "almalloc.h" -#include "alfstream.h" -#include "aloptional.h" -#include "alspan.h" -#include "alstring.h" -#include "compat.h" -#include "core/logging.h" -#include "strutils.h" -#include "vector.h" - - -/* Mixing thread piority level */ -int RTPrioLevel{1}; - -/* Allow reducing the process's RTTime limit for RTKit. */ -bool AllowRTTimeLimit{true}; - - -#ifdef _WIN32 - -#include - -const PathNamePair &GetProcBinary() -{ - static al::optional procbin; - if(procbin) return *procbin; - - auto fullpath = al::vector(256); - DWORD len; - while((len=GetModuleFileNameW(nullptr, fullpath.data(), static_cast(fullpath.size()))) == fullpath.size()) - fullpath.resize(fullpath.size() << 1); - if(len == 0) - { - ERR("Failed to get process name: error %lu\n", GetLastError()); - procbin = al::make_optional(); - return *procbin; - } - - fullpath.resize(len); - if(fullpath.back() != 0) - fullpath.push_back(0); - - auto sep = std::find(fullpath.rbegin()+1, fullpath.rend(), '\\'); - sep = std::find(fullpath.rbegin()+1, sep, '/'); - if(sep != fullpath.rend()) - { - *sep = 0; - procbin = al::make_optional(wstr_to_utf8(fullpath.data()), - wstr_to_utf8(&*sep + 1)); - } - else - procbin = al::make_optional(std::string{}, wstr_to_utf8(fullpath.data())); - - TRACE("Got binary: %s, %s\n", procbin->path.c_str(), procbin->fname.c_str()); - return *procbin; -} - -namespace { - -void DirectorySearch(const char *path, const char *ext, al::vector *const results) -{ - std::string pathstr{path}; - pathstr += "\\*"; - pathstr += ext; - TRACE("Searching %s\n", pathstr.c_str()); - - std::wstring wpath{utf8_to_wstr(pathstr.c_str())}; - WIN32_FIND_DATAW fdata; - HANDLE hdl{FindFirstFileW(wpath.c_str(), &fdata)}; - if(hdl == INVALID_HANDLE_VALUE) return; - - const auto base = results->size(); - - do { - results->emplace_back(); - std::string &str = results->back(); - str = path; - str += '\\'; - str += wstr_to_utf8(fdata.cFileName); - } while(FindNextFileW(hdl, &fdata)); - FindClose(hdl); - - const al::span newlist{results->data()+base, results->size()-base}; - std::sort(newlist.begin(), newlist.end()); - for(const auto &name : newlist) - TRACE(" got %s\n", name.c_str()); -} - -} // namespace - -al::vector SearchDataFiles(const char *ext, const char *subdir) -{ - auto is_slash = [](int c) noexcept -> int { return (c == '\\' || c == '/'); }; - - static std::mutex search_lock; - std::lock_guard _{search_lock}; - - /* If the path is absolute, use it directly. */ - al::vector results; - if(isalpha(subdir[0]) && subdir[1] == ':' && is_slash(subdir[2])) - { - std::string path{subdir}; - std::replace(path.begin(), path.end(), '/', '\\'); - DirectorySearch(path.c_str(), ext, &results); - return results; - } - if(subdir[0] == '\\' && subdir[1] == '\\' && subdir[2] == '?' && subdir[3] == '\\') - { - DirectorySearch(subdir, ext, &results); - return results; - } - - std::string path; - - /* Search the app-local directory. */ - if(auto localpath = al::getenv(L"ALSOFT_LOCAL_PATH")) - { - path = wstr_to_utf8(localpath->c_str()); - if(is_slash(path.back())) - path.pop_back(); - } - else if(WCHAR *cwdbuf{_wgetcwd(nullptr, 0)}) - { - path = wstr_to_utf8(cwdbuf); - if(is_slash(path.back())) - path.pop_back(); - free(cwdbuf); - } - else - path = "."; - std::replace(path.begin(), path.end(), '/', '\\'); - DirectorySearch(path.c_str(), ext, &results); - - /* Search the local and global data dirs. */ - static const int ids[2]{ CSIDL_APPDATA, CSIDL_COMMON_APPDATA }; - for(int id : ids) - { - WCHAR buffer[MAX_PATH]; - if(SHGetSpecialFolderPathW(nullptr, buffer, id, FALSE) == FALSE) - continue; - - path = wstr_to_utf8(buffer); - if(!is_slash(path.back())) - path += '\\'; - path += subdir; - std::replace(path.begin(), path.end(), '/', '\\'); - - DirectorySearch(path.c_str(), ext, &results); - } - - return results; -} - -void SetRTPriority(void) -{ - if(RTPrioLevel > 0) - { - if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL)) - ERR("Failed to set priority level for thread\n"); - } -} - -#else - -#include -#include -#include -#ifdef __FreeBSD__ -#include -#endif -#ifdef __HAIKU__ -#include -#endif -#ifdef HAVE_PROC_PIDPATH -#include -#endif -#if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__) -#include -#include -#endif -#ifdef HAVE_RTKIT -#include -#include - -#include "core/rtkit.h" -#ifndef RLIMIT_RTTIME -#define RLIMIT_RTTIME 15 -#endif -#endif - -const PathNamePair &GetProcBinary() -{ - static al::optional procbin; - if(procbin) return *procbin; - - al::vector pathname; -#ifdef __FreeBSD__ - size_t pathlen; - int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; - if(sysctl(mib, 4, nullptr, &pathlen, nullptr, 0) == -1) - WARN("Failed to sysctl kern.proc.pathname: %s\n", strerror(errno)); - else - { - pathname.resize(pathlen + 1); - sysctl(mib, 4, pathname.data(), &pathlen, nullptr, 0); - pathname.resize(pathlen); - } -#endif -#ifdef HAVE_PROC_PIDPATH - if(pathname.empty()) - { - char procpath[PROC_PIDPATHINFO_MAXSIZE]{}; - const pid_t pid{getpid()}; - if(proc_pidpath(pid, procpath, sizeof(procpath)) < 1) - ERR("proc_pidpath(%d, ...) failed: %s\n", pid, strerror(errno)); - else - pathname.insert(pathname.end(), procpath, procpath+strlen(procpath)); - } -#endif -#ifdef __HAIKU__ - if(pathname.empty()) - { - char procpath[PATH_MAX]; - if(find_path(B_APP_IMAGE_SYMBOL, B_FIND_PATH_IMAGE_PATH, NULL, procpath, sizeof(procpath)) == B_OK) - pathname.insert(pathname.end(), procpath, procpath+strlen(procpath)); - } -#endif -#ifndef __SWITCH__ - if(pathname.empty()) - { - static const char SelfLinkNames[][32]{ - "/proc/self/exe", - "/proc/self/file", - "/proc/curproc/exe", - "/proc/curproc/file" - }; - - pathname.resize(256); - - const char *selfname{}; - ssize_t len{}; - for(const char *name : SelfLinkNames) - { - selfname = name; - len = readlink(selfname, pathname.data(), pathname.size()); - if(len >= 0 || errno != ENOENT) break; - } - - while(len > 0 && static_cast(len) == pathname.size()) - { - pathname.resize(pathname.size() << 1); - len = readlink(selfname, pathname.data(), pathname.size()); - } - if(len <= 0) - { - WARN("Failed to readlink %s: %s\n", selfname, strerror(errno)); - len = 0; - } - - pathname.resize(static_cast(len)); - } -#endif - while(!pathname.empty() && pathname.back() == 0) - pathname.pop_back(); - - auto sep = std::find(pathname.crbegin(), pathname.crend(), '/'); - if(sep != pathname.crend()) - procbin = al::make_optional(std::string(pathname.cbegin(), sep.base()-1), - std::string(sep.base(), pathname.cend())); - else - procbin = al::make_optional(std::string{}, - std::string(pathname.cbegin(), pathname.cend())); - - TRACE("Got binary: \"%s\", \"%s\"\n", procbin->path.c_str(), procbin->fname.c_str()); - return *procbin; -} - -namespace { - -void DirectorySearch(const char *path, const char *ext, al::vector *const results) -{ - TRACE("Searching %s for *%s\n", path, ext); - DIR *dir{opendir(path)}; - if(!dir) return; - - const auto base = results->size(); - const size_t extlen{strlen(ext)}; - - while(struct dirent *dirent{readdir(dir)}) - { - if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0) - continue; - - const size_t len{strlen(dirent->d_name)}; - if(len <= extlen) continue; - if(al::strcasecmp(dirent->d_name+len-extlen, ext) != 0) - continue; - - results->emplace_back(); - std::string &str = results->back(); - str = path; - if(str.back() != '/') - str.push_back('/'); - str += dirent->d_name; - } - closedir(dir); - - const al::span newlist{results->data()+base, results->size()-base}; - std::sort(newlist.begin(), newlist.end()); - for(const auto &name : newlist) - TRACE(" got %s\n", name.c_str()); -} - -} // namespace - -al::vector SearchDataFiles(const char *ext, const char *subdir) -{ - static std::mutex search_lock; - std::lock_guard _{search_lock}; - - al::vector results; - if(subdir[0] == '/') - { - DirectorySearch(subdir, ext, &results); - return results; - } - - /* Search the app-local directory. */ - if(auto localpath = al::getenv("ALSOFT_LOCAL_PATH")) - DirectorySearch(localpath->c_str(), ext, &results); - else - { - al::vector cwdbuf(256); - while(!getcwd(cwdbuf.data(), cwdbuf.size())) - { - if(errno != ERANGE) - { - cwdbuf.clear(); - break; - } - cwdbuf.resize(cwdbuf.size() << 1); - } - if(cwdbuf.empty()) - DirectorySearch(".", ext, &results); - else - { - DirectorySearch(cwdbuf.data(), ext, &results); - cwdbuf.clear(); - } - } - - // Search local data dir - if(auto datapath = al::getenv("XDG_DATA_HOME")) - { - std::string &path = *datapath; - if(path.back() != '/') - path += '/'; - path += subdir; - DirectorySearch(path.c_str(), ext, &results); - } - else if(auto homepath = al::getenv("HOME")) - { - std::string &path = *homepath; - if(path.back() == '/') - path.pop_back(); - path += "/.local/share/"; - path += subdir; - DirectorySearch(path.c_str(), ext, &results); - } - - // Search global data dirs - std::string datadirs{al::getenv("XDG_DATA_DIRS").value_or("/usr/local/share/:/usr/share/")}; - - size_t curpos{0u}; - while(curpos < datadirs.size()) - { - size_t nextpos{datadirs.find(':', curpos)}; - - std::string path{(nextpos != std::string::npos) ? - datadirs.substr(curpos, nextpos++ - curpos) : datadirs.substr(curpos)}; - curpos = nextpos; - - if(path.empty()) continue; - if(path.back() != '/') - path += '/'; - path += subdir; - - DirectorySearch(path.c_str(), ext, &results); - } - - return results; -} - -void SetRTPriority() -{ - if(RTPrioLevel <= 0) - return; - - int err{-ENOTSUP}; -#if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__) - struct sched_param param{}; - /* Use the minimum real-time priority possible for now (on Linux this - * should be 1 for SCHED_RR). - */ - param.sched_priority = sched_get_priority_min(SCHED_RR); -#ifdef SCHED_RESET_ON_FORK - err = pthread_setschedparam(pthread_self(), SCHED_RR|SCHED_RESET_ON_FORK, ¶m); - if(err == EINVAL) -#endif - err = pthread_setschedparam(pthread_self(), SCHED_RR, ¶m); - if(err == 0) return; - - WARN("pthread_setschedparam failed: %s (%d)\n", std::strerror(err), err); -#endif -#ifdef HAVE_RTKIT - if(HasDBus()) - { - dbus::Error error; - if(dbus::ConnectionPtr conn{(*pdbus_bus_get)(DBUS_BUS_SYSTEM, &error.get())}) - { - using ulonglong = unsigned long long; - auto limit_rttime = [](DBusConnection *conn) -> int - { - long long maxrttime{rtkit_get_rttime_usec_max(conn)}; - if(maxrttime <= 0) return static_cast(std::abs(maxrttime)); - const ulonglong umaxtime{static_cast(maxrttime)}; - - struct rlimit rlim{}; - if(getrlimit(RLIMIT_RTTIME, &rlim) != 0) - return errno; - TRACE("RTTime max: %llu (hard: %llu, soft: %llu)\n", umaxtime, - ulonglong{rlim.rlim_max}, ulonglong{rlim.rlim_cur}); - if(rlim.rlim_max > umaxtime) - { - rlim.rlim_max = static_cast(std::min(umaxtime, - std::numeric_limits::max())); - rlim.rlim_cur = std::min(rlim.rlim_cur, rlim.rlim_max); - if(setrlimit(RLIMIT_RTTIME, &rlim) != 0) - return errno; - } - return 0; - }; - - /* Don't stupidly exit if the connection dies while doing this. */ - (*pdbus_connection_set_exit_on_disconnect)(conn.get(), false); - - int nicemin{}; - err = rtkit_get_min_nice_level(conn.get(), &nicemin); - if(err == -ENOENT) - { - err = std::abs(err); - ERR("Could not query RTKit: %s (%d)\n", std::strerror(err), err); - return; - } - int rtmax{rtkit_get_max_realtime_priority(conn.get())}; - TRACE("Maximum real-time priority: %d, minimum niceness: %d\n", rtmax, nicemin); - - err = EINVAL; - if(rtmax > 0) - { - if(AllowRTTimeLimit) - { - err = limit_rttime(conn.get()); - if(err != 0) - WARN("Failed to set RLIMIT_RTTIME for RTKit: %s (%d)\n", - std::strerror(err), err); - } - - /* Use half the maximum real-time priority allowed. */ - TRACE("Making real-time with priority %d\n", (rtmax+1)/2); - err = rtkit_make_realtime(conn.get(), 0, (rtmax+1)/2); - if(err == 0) return; - - err = std::abs(err); - WARN("Failed to set real-time priority: %s (%d)\n", std::strerror(err), err); - } - if(nicemin < 0) - { - TRACE("Making high priority with niceness %d\n", nicemin); - err = rtkit_make_high_priority(conn.get(), 0, nicemin); - if(err == 0) return; - - err = std::abs(err); - WARN("Failed to set high priority: %s (%d)\n", std::strerror(err), err); - } - } - else - WARN("D-Bus connection failed with %s: %s\n", error->name, error->message); - } - else - WARN("D-Bus not available\n"); -#endif - ERR("Could not set elevated priority: %s (%d)\n", std::strerror(err), err); -} - -#endif diff --git a/alc/hrtf.cpp b/alc/hrtf.cpp index 6d7bc949..6030bb65 100644 --- a/alc/hrtf.cpp +++ b/alc/hrtf.cpp @@ -48,9 +48,9 @@ #include "alnumeric.h" #include "aloptional.h" #include "alspan.h" -#include "compat.h" #include "core/ambidefs.h" #include "core/filters/splitter.h" +#include "core/helpers.h" #include "core/logging.h" #include "math_defs.h" #include "opthelpers.h" -- cgit v1.2.3