Use native APIs instead of env vars or finding Windows paths

Fixes #202

Signed-off-by: falkTX <falktx@falktx.com>
This commit is contained in:
falkTX 2022-03-26 16:07:25 +00:00
parent c411946ccf
commit 56305eb037
3 changed files with 41 additions and 13 deletions

View file

@ -43,8 +43,10 @@
# undef DEBUG # undef DEBUG
#endif #endif
// for finding home dir // for finding special paths
#ifndef ARCH_WIN #ifdef ARCH_WIN
# include <shlobj.h>
#else
# include <pwd.h> # include <pwd.h>
# include <unistd.h> # include <unistd.h>
#endif #endif
@ -55,6 +57,31 @@ namespace rack {
namespace settings { namespace settings {
int rateLimit = 0; int rateLimit = 0;
} }
#ifdef ARCH_WIN
std::string getSpecialPath(const SpecialPath type)
{
int csidl;
switch (type)
{
case kSpecialPathUserProfile:
csidl = CSIDL_PROFILE;
break;
case kSpecialPathCommonProgramFiles:
csidl = CSIDL_PROGRAM_FILES_COMMON;
break;
default:
return {};
}
WCHAR path[MAX_PATH + 256];
if (SHGetSpecialFolderPathW(nullptr, path, csidl, FALSE))
return string::UTF16toUTF8(path);
return {};
}
#endif
} }
namespace patchUtils namespace patchUtils
@ -74,19 +101,11 @@ static void promptClear(const char* const message, const std::function<void()> a
static std::string homeDir() static std::string homeDir()
{ {
# ifdef ARCH_WIN # ifdef ARCH_WIN
if (const char* const userprofile = getenv("USERPROFILE")) return getSpecialPath(kSpecialPathUserProfile);
{
return userprofile;
}
else if (const char* const homedrive = getenv("HOMEDRIVE"))
{
if (const char* const homepath = getenv("HOMEPATH"))
return system::join(homedrive, homepath);
}
# else # else
if (const char* const home = getenv("HOME")) if (const char* const home = getenv("HOME"))
return home; return home;
else if (struct passwd* const pwd = getpwuid(getuid())) if (struct passwd* const pwd = getpwuid(getuid()))
return pwd->pw_dir; return pwd->pw_dir;
# endif # endif
return {}; return {};

View file

@ -41,6 +41,14 @@ namespace window {
void generateScreenshot(); void generateScreenshot();
} }
#ifdef ARCH_WIN
enum SpecialPath {
kSpecialPathUserProfile,
kSpecialPathCommonProgramFiles,
};
std::string getSpecialPath(SpecialPath type);
#endif
} // namespace rack } // namespace rack
namespace patchUtils { namespace patchUtils {

View file

@ -155,7 +155,8 @@ struct Initializer
#if defined(ARCH_MAC) #if defined(ARCH_MAC)
asset::systemDir = "/Library/Application Support/Cardinal"; asset::systemDir = "/Library/Application Support/Cardinal";
#elif defined(ARCH_WIN) #elif defined(ARCH_WIN)
if (const char* const commonprogfiles = std::getenv("COMMONPROGRAMFILES")) const std::string commonprogfiles = getSpecialPath(kSpecialPathCommonProgramFiles);
if (! commonprogfiles.empty())
asset::systemDir = system::join(commonprogfiles, "Cardinal"); asset::systemDir = system::join(commonprogfiles, "Cardinal");
#else #else
asset::systemDir = CARDINAL_PLUGIN_PREFIX "/share/cardinal"; asset::systemDir = CARDINAL_PLUGIN_PREFIX "/share/cardinal";