You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.1KB

  1. #include "global_pre.hpp"
  2. #include "asset.hpp"
  3. #include "util/common.hpp"
  4. #include "osdialog.h"
  5. #include "global.hpp"
  6. #if ARCH_MAC
  7. #include <CoreFoundation/CoreFoundation.h>
  8. #include <pwd.h>
  9. #endif
  10. #if ARCH_WIN
  11. #include <Windows.h>
  12. #include <Shlobj.h>
  13. #include <Shlwapi.h>
  14. #endif
  15. #if ARCH_LIN
  16. #include <unistd.h>
  17. #include <sys/types.h>
  18. #include <pwd.h>
  19. #endif
  20. namespace rack {
  21. void assetInit(bool devMode) {
  22. #ifndef USE_VST2
  23. if (devMode) {
  24. // Use current working directory if running in development mode
  25. global->asset.globalDir = ".";
  26. global->asset.localDir = ".";
  27. return;
  28. }
  29. #endif // USE_VST2
  30. #if ARCH_MAC
  31. CFBundleRef bundle = CFBundleGetMainBundle();
  32. assert(bundle);
  33. CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
  34. char resourcesBuf[PATH_MAX];
  35. Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf));
  36. assert(success);
  37. CFRelease(resourcesUrl);
  38. global->asset.globalDir = resourcesBuf;
  39. // Get home directory
  40. struct passwd *pw = getpwuid(getuid());
  41. assert(pw);
  42. global->asset.localDir = pw->pw_dir;
  43. global->asset.localDir += "/Documents/Rack";
  44. #endif
  45. #if ARCH_WIN
  46. #ifndef USE_VST2
  47. char moduleBuf[MAX_PATH];
  48. DWORD length = GetModuleFileName(NULL, moduleBuf, sizeof(moduleBuf));
  49. assert(length > 0);
  50. PathRemoveFileSpec(moduleBuf);
  51. global->asset.globalDir = moduleBuf;
  52. // Get "My Documents" folder
  53. char documentsBuf[MAX_PATH];
  54. HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documentsBuf);
  55. assert(result == S_OK);
  56. global->asset.localDir = documentsBuf;
  57. global->asset.localDir += "/Rack";
  58. #else
  59. global->asset.globalDir = global->vst2.program_dir;
  60. global->asset.localDir = global->vst2.program_dir;
  61. #endif // USE_VST2
  62. #endif
  63. #if ARCH_LIN
  64. #ifndef USE_VST2
  65. // TODO For now, users should launch Rack from their terminal in the global directory
  66. global->asset.globalDir = ".";
  67. // Get home directory
  68. const char *homeBuf = getenv("HOME");
  69. if (!homeBuf) {
  70. struct passwd *pw = getpwuid(getuid());
  71. assert(pw);
  72. homeBuf = pw->pw_dir;
  73. }
  74. global->asset.localDir = homeBuf;
  75. global->asset.localDir += "/.Rack";
  76. #else
  77. global->asset.globalDir = global->vst2.program_dir;
  78. global->asset.localDir = global->vst2.program_dir;
  79. #endif // USE_VST2
  80. #endif
  81. systemCreateDirectory(global->asset.localDir);
  82. }
  83. std::string assetGlobal(std::string filename) {
  84. return global->asset.globalDir + "/" + filename;
  85. }
  86. std::string assetLocal(std::string filename) {
  87. return global->asset.localDir + "/" + filename;
  88. }
  89. std::string assetPlugin(Plugin *plugin, std::string filename) {
  90. printf("xxx assetPlugin(plugin=%p)\n", plugin);
  91. printf("xxx assetPlugin: filename=\"%s\"\n", filename.c_str());
  92. assert(plugin);
  93. return plugin->path + "/" + filename;
  94. }
  95. #ifdef USE_VST2
  96. std::string assetStaticPlugin(const char *name/*e.g. "Fundamentals"*/, const char *_relPathOrNull) {
  97. return global->asset.localDir + "plugins/" + name + "/" + ((NULL != _relPathOrNull)?_relPathOrNull:"");
  98. }
  99. std::string assetPlugin(const char *name/*e.g. "Fundamentals"*/, const char *_relPathOrNull) {
  100. return assetStaticPlugin(name, _relPathOrNull);
  101. }
  102. #endif // USE_VST2
  103. } // namespace rack