aboutsummaryrefslogtreecommitdiffstats
path: root/core/helpers.cpp
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2023-11-28 12:51:46 +0100
committerSven Gothel <[email protected]>2023-11-28 12:51:46 +0100
commit1aaf4f070011490bcece50394b9b32dfa593fd9e (patch)
tree17d68284e401a35eea3d3a574d986d446a60763a /core/helpers.cpp
parent6e7cee4fa9a8af03f28ca26cd89f8357390dfc90 (diff)
parent571b546f35eead77ce109f8d4dd6c3de3199d573 (diff)
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'core/helpers.cpp')
-rw-r--r--core/helpers.cpp86
1 files changed, 45 insertions, 41 deletions
diff --git a/core/helpers.cpp b/core/helpers.cpp
index 99cf009c..5a996eee 100644
--- a/core/helpers.cpp
+++ b/core/helpers.cpp
@@ -3,29 +3,27 @@
#include "helpers.h"
+#if defined(_WIN32)
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#endif
+
#include <algorithm>
-#include <cerrno>
-#include <cstdarg>
#include <cstdlib>
-#include <cstdio>
#include <cstring>
-#include <mutex>
#include <limits>
+#include <mutex>
+#include <optional>
#include <string>
-#include <tuple>
-#include "almalloc.h"
-#include "alfstream.h"
#include "alnumeric.h"
-#include "aloptional.h"
#include "alspan.h"
#include "alstring.h"
#include "logging.h"
#include "strutils.h"
-#include "vector.h"
-/* Mixing thread piority level */
+/* Mixing thread priority level */
int RTPrioLevel{1};
/* Allow reducing the process's RTTime limit for RTKit. */
@@ -34,14 +32,15 @@ bool AllowRTTimeLimit{true};
#ifdef _WIN32
+#include <cctype>
#include <shlobj.h>
const PathNamePair &GetProcBinary()
{
- static al::optional<PathNamePair> procbin;
+ static std::optional<PathNamePair> procbin;
if(procbin) return *procbin;
-
- auto fullpath = al::vector<WCHAR>(256);
+#if !defined(ALSOFT_UWP)
+ auto fullpath = std::vector<WCHAR>(256);
DWORD len{GetModuleFileNameW(nullptr, fullpath.data(), static_cast<DWORD>(fullpath.size()))};
while(len == fullpath.size())
{
@@ -58,7 +57,16 @@ const PathNamePair &GetProcBinary()
fullpath.resize(len);
if(fullpath.back() != 0)
fullpath.push_back(0);
-
+#else
+ auto exePath = __wargv[0];
+ if (!exePath)
+ {
+ ERR("Failed to get process name: error %lu\n", GetLastError());
+ procbin.emplace();
+ return *procbin;
+ }
+ std::vector<WCHAR> fullpath{exePath, exePath + wcslen(exePath) + 1};
+#endif
std::replace(fullpath.begin(), fullpath.end(), '/', '\\');
auto sep = std::find(fullpath.rbegin()+1, fullpath.rend(), '\\');
if(sep != fullpath.rend())
@@ -75,16 +83,16 @@ const PathNamePair &GetProcBinary()
namespace {
-void DirectorySearch(const char *path, const char *ext, al::vector<std::string> *const results)
+void DirectorySearch(const char *path, const char *ext, std::vector<std::string> *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())};
+ std::wstring wpath{utf8_to_wstr(pathstr)};
WIN32_FIND_DATAW fdata;
- HANDLE hdl{FindFirstFileW(wpath.c_str(), &fdata)};
+ HANDLE hdl{FindFirstFileExW(wpath.c_str(), FindExInfoStandard, &fdata, FindExSearchNameMatch, NULL, 0)};
if(hdl == INVALID_HANDLE_VALUE) return;
const auto base = results->size();
@@ -97,7 +105,6 @@ void DirectorySearch(const char *path, const char *ext, al::vector<std::string>
str += wstr_to_utf8(fdata.cFileName);
} while(FindNextFileW(hdl, &fdata));
FindClose(hdl);
-
const al::span<std::string> newlist{results->data()+base, results->size()-base};
std::sort(newlist.begin(), newlist.end());
for(const auto &name : newlist)
@@ -106,16 +113,16 @@ void DirectorySearch(const char *path, const char *ext, al::vector<std::string>
} // namespace
-al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
+std::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
{
- auto is_slash = [](int c) noexcept -> int { return (c == '\\' || c == '/'); };
+ auto is_slash = [](int c) noexcept { return (c == '\\' || c == '/'); };
static std::mutex search_lock;
std::lock_guard<std::mutex> _{search_lock};
/* If the path is absolute, use it directly. */
- al::vector<std::string> results;
- if(isalpha(subdir[0]) && subdir[1] == ':' && is_slash(subdir[2]))
+ std::vector<std::string> results;
+ if(std::isalpha(subdir[0]) && subdir[1] == ':' && is_slash(subdir[2]))
{
std::string path{subdir};
std::replace(path.begin(), path.end(), '/', '\\');
@@ -149,9 +156,9 @@ al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
std::replace(path.begin(), path.end(), '/', '\\');
DirectorySearch(path.c_str(), ext, &results);
+#if !defined(ALSOFT_UWP) && !defined(_GAMING_XBOX)
/* Search the local and global data dirs. */
- static const int ids[2]{ CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
- for(int id : ids)
+ for(auto id : std::array{CSIDL_APPDATA, CSIDL_COMMON_APPDATA})
{
WCHAR buffer[MAX_PATH];
if(SHGetSpecialFolderPathW(nullptr, buffer, id, FALSE) == FALSE)
@@ -165,24 +172,27 @@ al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
DirectorySearch(path.c_str(), ext, &results);
}
+#endif
return results;
}
void SetRTPriority(void)
{
+#if !defined(ALSOFT_UWP)
if(RTPrioLevel > 0)
{
if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL))
ERR("Failed to set priority level for thread\n");
}
+#endif
}
#else
-#include <sys/types.h>
-#include <unistd.h>
+#include <cerrno>
#include <dirent.h>
+#include <unistd.h>
#ifdef __FreeBSD__
#include <sys/sysctl.h>
#endif
@@ -197,7 +207,6 @@ void SetRTPriority(void)
#include <sched.h>
#endif
#ifdef HAVE_RTKIT
-#include <sys/time.h>
#include <sys/resource.h>
#include "dbus_wrap.h"
@@ -209,10 +218,10 @@ void SetRTPriority(void)
const PathNamePair &GetProcBinary()
{
- static al::optional<PathNamePair> procbin;
+ static std::optional<PathNamePair> procbin;
if(procbin) return *procbin;
- al::vector<char> pathname;
+ std::vector<char> pathname;
#ifdef __FreeBSD__
size_t pathlen;
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
@@ -247,7 +256,7 @@ const PathNamePair &GetProcBinary()
#ifndef __SWITCH__
if(pathname.empty())
{
- static const char SelfLinkNames[][32]{
+ const char *SelfLinkNames[]{
"/proc/self/exe",
"/proc/self/file",
"/proc/curproc/exe",
@@ -295,7 +304,7 @@ const PathNamePair &GetProcBinary()
namespace {
-void DirectorySearch(const char *path, const char *ext, al::vector<std::string> *const results)
+void DirectorySearch(const char *path, const char *ext, std::vector<std::string> *const results)
{
TRACE("Searching %s for *%s\n", path, ext);
DIR *dir{opendir(path)};
@@ -331,12 +340,12 @@ void DirectorySearch(const char *path, const char *ext, al::vector<std::string>
} // namespace
-al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
+std::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
{
static std::mutex search_lock;
std::lock_guard<std::mutex> _{search_lock};
- al::vector<std::string> results;
+ std::vector<std::string> results;
if(subdir[0] == '/')
{
DirectorySearch(subdir, ext, &results);
@@ -348,7 +357,7 @@ al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
DirectorySearch(localpath->c_str(), ext, &results);
else
{
- al::vector<char> cwdbuf(256);
+ std::vector<char> cwdbuf(256);
while(!getcwd(cwdbuf.data(), cwdbuf.size()))
{
if(errno != ERANGE)
@@ -425,7 +434,7 @@ al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
namespace {
-bool SetRTPriorityPthread(int prio)
+bool SetRTPriorityPthread(int prio [[maybe_unused]])
{
int err{ENOTSUP};
#if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
@@ -445,16 +454,12 @@ bool SetRTPriorityPthread(int prio)
#endif
err = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
if(err == 0) return true;
-
-#else
-
- std::ignore = prio;
#endif
WARN("pthread_setschedparam failed: %s (%d)\n", std::strerror(err), err);
return false;
}
-bool SetRTPriorityRTKit(int prio)
+bool SetRTPriorityRTKit(int prio [[maybe_unused]])
{
#ifdef HAVE_RTKIT
if(!HasDBus())
@@ -547,7 +552,6 @@ bool SetRTPriorityRTKit(int prio)
#else
- std::ignore = prio;
WARN("D-Bus not supported\n");
#endif
return false;