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.

119 lines
2.5KB

  1. #include "engine.hpp"
  2. #include "gui.hpp"
  3. #include "app.hpp"
  4. #include "plugin.hpp"
  5. #include "settings.hpp"
  6. #if ARCH_MAC
  7. #include <CoreFoundation/CoreFoundation.h>
  8. #include <unistd.h> // for chdir and access
  9. #include <libgen.h> // for dirname
  10. // #include <string.h>
  11. #include <mach-o/dyld.h> // for _NSGetExecutablePath
  12. #include <limits.h> // for PATH_MAX?
  13. #include <dirent.h> // for opendir
  14. void alert(std::string header, std::string message, int level) {
  15. CFStringRef headerRef = CFStringCreateWithCString(NULL, header.c_str(), header.size());
  16. CFStringRef messageRef = CFStringCreateWithCString(NULL, message.c_str(), message.size());
  17. CFOptionFlags result;
  18. CFUserNotificationDisplayAlert(
  19. 0, // no timeout
  20. level, // flags for alert level
  21. NULL, // iconURL
  22. NULL, // soundURL
  23. NULL, // localizationURL
  24. headerRef,
  25. messageRef,
  26. NULL, // default "OK"
  27. NULL, // alternative button
  28. NULL, // other button
  29. &result
  30. );
  31. CFRelease(headerRef);
  32. CFRelease(messageRef);
  33. }
  34. bool isCorrectCwd() {
  35. DIR *dir = opendir("res");
  36. if (dir) {
  37. closedir(dir);
  38. return true;
  39. }
  40. else {
  41. return false;
  42. }
  43. }
  44. /** macOS workaround for setting the working directory to the location of the .app */
  45. void fixCwd() {
  46. // Check if the cwd is already set correctly (e.g. launched from the command line or gdb)
  47. if (isCorrectCwd())
  48. return;
  49. /*
  50. // Get path of binary inside the app bundle
  51. // It should be something like .../Rack.app/Contents/MacOS
  52. char path[PATH_MAX];
  53. uint32_t pathLen = sizeof(path);
  54. int err = _NSGetExecutablePath(path, &pathLen);
  55. assert(!err);
  56. if (isCorrectCwd())
  57. return;
  58. // Switch to the directory of the actual binary
  59. chdir(dirname(path));
  60. if (isCorrectCwd())
  61. return;
  62. // and then go up three directories to get to the parent directory
  63. chdir("../../../");
  64. if (isCorrectCwd())
  65. return;
  66. */
  67. // Switch to a default absolute path
  68. chdir("/Applications/Rack");
  69. if (isCorrectCwd())
  70. return;
  71. alert("Install Rack", "To install Rack, please move the Rack directory (including the Rack app and plugins directory) to the /Applications folder.", 2);
  72. exit(1);
  73. }
  74. #endif
  75. using namespace rack;
  76. int main(int argc, char* argv[]) {
  77. #if ARCH_MAC
  78. fixCwd();
  79. #endif
  80. pluginInit();
  81. engineInit();
  82. guiInit();
  83. sceneInit();
  84. settingsLoad("settings.json");
  85. if (argc >= 2)
  86. gRackWidget->loadPatch(argv[1]);
  87. else
  88. gRackWidget->loadPatch("autosave.json");
  89. engineStart();
  90. guiRun();
  91. engineStop();
  92. gRackWidget->savePatch("autosave.json");
  93. settingsSave("settings.json");
  94. sceneDestroy();
  95. guiDestroy();
  96. engineDestroy();
  97. pluginDestroy();
  98. return 0;
  99. }