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.

100 lines
2.1KB

  1. #include "asset.hpp"
  2. #include "util.hpp"
  3. #include <assert.h>
  4. #include <sys/stat.h> // for mkdir
  5. #include "../ext/osdialog/osdialog.h"
  6. #if ARCH_MAC
  7. #include <CoreFoundation/CoreFoundation.h>
  8. #include <pwd.h>
  9. #endif
  10. namespace rack {
  11. #if ARCH_MAC
  12. /** Is it actually difficult to determine whether we are running in a Mac bundle or not.
  13. This heuristically guesses based on the existence of a Resources directory
  14. */
  15. static bool isBundle() {
  16. CFBundleRef bundle = CFBundleGetMainBundle();
  17. if (bundle) {
  18. CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
  19. char buf[PATH_MAX];
  20. Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8 *)buf, sizeof(buf));
  21. assert(success);
  22. CFRelease(resourcesUrl);
  23. if (extractFilename(buf) == "Resources")
  24. return true;
  25. }
  26. return false;
  27. }
  28. #endif
  29. std::string assetGlobal(std::string filename) {
  30. std::string path;
  31. #if ARCH_MAC
  32. CFBundleRef bundle = CFBundleGetMainBundle();
  33. if (bundle && isBundle()) {
  34. CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
  35. char buf[PATH_MAX];
  36. Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8 *)buf, sizeof(buf));
  37. assert(success);
  38. CFRelease(resourcesUrl);
  39. path = buf;
  40. }
  41. else {
  42. path = ".";
  43. }
  44. path += "/" + filename;
  45. #endif
  46. #if ARCH_WIN
  47. path = "./" + filename;
  48. #endif
  49. #if ARCH_LIN
  50. path = "./" + filename;
  51. #endif
  52. return path;
  53. }
  54. std::string assetLocal(std::string filename) {
  55. std::string path;
  56. #if ARCH_MAC
  57. if (isBundle()) {
  58. // Get home directory
  59. struct passwd *pw = getpwuid(getuid());
  60. assert(pw);
  61. path = pw->pw_dir;
  62. path += "/Documents/Rack";
  63. mkdir(path.c_str(), 0755);
  64. }
  65. else {
  66. path = ".";
  67. }
  68. path += "/" + filename;
  69. #endif
  70. #if ARCH_WIN
  71. // TODO
  72. // Use ~/My Documents/Rack or something
  73. path = "./" + filename;
  74. #endif
  75. #if ARCH_LIN
  76. // TODO
  77. // If Rack is "installed" (however that may be defined), look in ~/.Rack or something instead
  78. path = "./" + filename;
  79. #endif
  80. return path;
  81. }
  82. std::string assetPlugin(Plugin *plugin, std::string filename) {
  83. assert(plugin);
  84. std::string path;
  85. path = plugin->path + "/" + filename;
  86. return path;
  87. }
  88. } // namespace rack