diff --git a/src/CardinalCommon.cpp b/src/CardinalCommon.cpp index 5945b9f..1670a8b 100644 --- a/src/CardinalCommon.cpp +++ b/src/CardinalCommon.cpp @@ -43,8 +43,10 @@ # undef DEBUG #endif -// for finding home dir -#ifndef ARCH_WIN +// for finding special paths +#ifdef ARCH_WIN +# include +#else # include # include #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 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 {}; diff --git a/src/CardinalCommon.hpp b/src/CardinalCommon.hpp index 7c9a74f..961c340 100644 --- a/src/CardinalCommon.hpp +++ b/src/CardinalCommon.hpp @@ -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 { diff --git a/src/CardinalPlugin.cpp b/src/CardinalPlugin.cpp index b41be7a..c5779f2 100644 --- a/src/CardinalPlugin.cpp +++ b/src/CardinalPlugin.cpp @@ -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";