1
Fork 0

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
#endif
// for finding home dir
#ifndef ARCH_WIN
// for finding special paths
#ifdef ARCH_WIN
# include <shlobj.h>
#else
# include <pwd.h>
# include <unistd.h>
#endif
@ -55,6 +57,31 @@ namespace rack {
namespace settings {
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
@ -74,19 +101,11 @@ static void promptClear(const char* const message, const std::function<void()> a
static std::string homeDir()
{
# ifdef ARCH_WIN
if (const char* const userprofile = getenv("USERPROFILE"))
{
return userprofile;
}
else if (const char* const homedrive = getenv("HOMEDRIVE"))
{
if (const char* const homepath = getenv("HOMEPATH"))
return system::join(homedrive, homepath);
}
return getSpecialPath(kSpecialPathUserProfile);
# else
if (const char* const home = getenv("HOME"))
return home;
else if (struct passwd* const pwd = getpwuid(getuid()))
if (struct passwd* const pwd = getpwuid(getuid()))
return pwd->pw_dir;
# endif
return {};

View File

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

View File

@ -155,7 +155,8 @@ struct Initializer
#if defined(ARCH_MAC)
asset::systemDir = "/Library/Application Support/Cardinal";
#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");
#else
asset::systemDir = CARDINAL_PLUGIN_PREFIX "/share/cardinal";