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.

261 lines
6.5KB

  1. #include <common.hpp>
  2. #include <random.hpp>
  3. #include <asset.hpp>
  4. #include <audio.hpp>
  5. #include <rtaudio.hpp>
  6. #include <midi.hpp>
  7. #include <rtmidi.hpp>
  8. #include <keyboard.hpp>
  9. #include <gamepad.hpp>
  10. #include <settings.hpp>
  11. #include <engine/Engine.hpp>
  12. #include <app/common.hpp>
  13. #include <app/Scene.hpp>
  14. #include <plugin.hpp>
  15. #include <context.hpp>
  16. #include <Window.hpp>
  17. #include <patch.hpp>
  18. #include <history.hpp>
  19. #include <ui/common.hpp>
  20. #include <system.hpp>
  21. #include <string.hpp>
  22. #include <library.hpp>
  23. #include <network.hpp>
  24. #include <osdialog.h>
  25. #include <thread>
  26. #include <unistd.h> // for getopt
  27. #include <signal.h> // for signal
  28. #if defined ARCH_WIN
  29. #include <windows.h> // for CreateMutex
  30. #endif
  31. #if defined ARCH_MAC
  32. #define GLFW_EXPOSE_NATIVE_COCOA
  33. #include <GLFW/glfw3native.h> // for glfwGetOpenedFilenames()
  34. #endif
  35. using namespace rack;
  36. static void fatalSignalHandler(int sig) {
  37. // Ignore this signal to avoid recursion.
  38. signal(sig, NULL);
  39. // Ignore abort() since we call it below.
  40. signal(SIGABRT, NULL);
  41. FATAL("Fatal signal %d. Stack trace:\n%s", sig, system::getStackTrace().c_str());
  42. // This might fail because we might not be in the main thread.
  43. // But oh well, we're crashing anyway.
  44. std::string text = APP_NAME + " has crashed. See " + logger::logPath + " for details.";
  45. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, text.c_str());
  46. abort();
  47. }
  48. // TODO Use -municode on Windows and change this to wmain. Convert argv to UTF-8.
  49. int main(int argc, char* argv[]) {
  50. #if defined ARCH_WIN
  51. // Windows global mutex to prevent multiple instances
  52. // Handle will be closed by Windows when the process ends
  53. HANDLE instanceMutex = CreateMutexW(NULL, true, string::UTF8toUTF16(APP_NAME).c_str());
  54. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  55. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Rack is already running. Multiple Rack instances are not supported.");
  56. exit(1);
  57. }
  58. (void) instanceMutex;
  59. // Don't display "Assertion failed!" dialog message.
  60. _set_error_mode(_OUT_TO_STDERR);
  61. #endif
  62. std::string patchPath;
  63. bool screenshot = false;
  64. float screenshotZoom = 1.f;
  65. // Parse command line arguments
  66. int c;
  67. opterr = 0;
  68. while ((c = getopt(argc, argv, "dht:s:u:p:")) != -1) {
  69. switch (c) {
  70. case 'd': {
  71. settings::devMode = true;
  72. } break;
  73. case 'h': {
  74. settings::headless = true;
  75. } break;
  76. case 't': {
  77. screenshot = true;
  78. std::sscanf(optarg, "%f", &screenshotZoom);
  79. } break;
  80. case 's': {
  81. asset::systemDir = optarg;
  82. } break;
  83. case 'u': {
  84. asset::userDir = optarg;
  85. } break;
  86. // Mac "app translocation" passes a nonsense -psn_... flag, so -p is reserved.
  87. case 'p': break;
  88. default: break;
  89. }
  90. }
  91. if (optind < argc) {
  92. patchPath = argv[optind];
  93. }
  94. // Initialize environment
  95. system::init();
  96. asset::init();
  97. bool loggerWasTruncated = logger::isTruncated();
  98. logger::init();
  99. random::init();
  100. // Test code
  101. // exit(0);
  102. // We can now install a signal handler and log the output
  103. if (!settings::devMode) {
  104. signal(SIGABRT, fatalSignalHandler);
  105. signal(SIGFPE, fatalSignalHandler);
  106. signal(SIGILL, fatalSignalHandler);
  107. signal(SIGSEGV, fatalSignalHandler);
  108. signal(SIGTERM, fatalSignalHandler);
  109. }
  110. // Log environment
  111. INFO("%s %s v%s", APP_NAME.c_str(), APP_VARIANT.c_str(), APP_VERSION.c_str());
  112. INFO("%s", system::getOperatingSystemInfo().c_str());
  113. std::string argsList;
  114. for (int i = 0; i < argc; i++) {
  115. argsList += argv[i];
  116. argsList += " ";
  117. }
  118. INFO("Args: %s", argsList.c_str());
  119. if (settings::devMode)
  120. INFO("Development mode");
  121. INFO("System directory: %s", asset::systemDir.c_str());
  122. INFO("User directory: %s", asset::userDir.c_str());
  123. #if defined ARCH_MAC
  124. INFO("Bundle path: %s", asset::bundlePath.c_str());
  125. #endif
  126. // Load settings
  127. settings::init();
  128. try {
  129. settings::load();
  130. }
  131. catch (Exception& e) {
  132. std::string msg = e.what();
  133. msg += "\n\nReset settings to default?";
  134. if (!osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK_CANCEL, msg.c_str())) {
  135. exit(1);
  136. }
  137. }
  138. // Check existence of the system res/ directory
  139. std::string resDir = asset::system("res");
  140. if (!system::isDirectory(resDir)) {
  141. std::string message = string::f("Rack's resource directory \"%s\" does not exist. Make sure Rack is correctly installed and launched.", resDir.c_str());
  142. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, message.c_str());
  143. exit(1);
  144. }
  145. INFO("Initializing environment");
  146. network::init();
  147. audio::init();
  148. rtaudioInit();
  149. midi::init();
  150. rtmidiInit();
  151. keyboard::init();
  152. gamepad::init();
  153. plugin::init();
  154. library::init();
  155. if (!settings::headless) {
  156. ui::init();
  157. windowInit();
  158. }
  159. // Initialize context
  160. INFO("Initializing context");
  161. contextSet(new Context);
  162. APP->engine = new engine::Engine;
  163. APP->history = new history::State;
  164. APP->event = new widget::EventState;
  165. APP->scene = new app::Scene;
  166. APP->event->rootWidget = APP->scene;
  167. APP->patch = new PatchManager;
  168. if (!settings::headless) {
  169. APP->window = new Window;
  170. }
  171. // On Mac, use a hacked-in GLFW addition to get the launched path.
  172. #if defined ARCH_MAC
  173. // For some reason, launching from the command line sets glfwGetOpenedFilenames(), so make sure we're running the app bundle.
  174. if (asset::bundlePath != "") {
  175. // const char* const* openedFilenames = glfwGetOpenedFilenames();
  176. // if (openedFilenames && openedFilenames[0]) {
  177. // patchPath = openedFilenames[0];
  178. // }
  179. }
  180. #endif
  181. // Initialize patch
  182. if (loggerWasTruncated && osdialog_message(OSDIALOG_INFO, OSDIALOG_YES_NO, "Rack crashed during the last session, possibly due to a buggy module in your patch. Clear your patch and start over?")) {
  183. // Do nothing
  184. }
  185. else {
  186. APP->patch->launch(patchPath);
  187. }
  188. // Run context
  189. if (settings::headless) {
  190. printf("Press enter to exit.\n");
  191. getchar();
  192. }
  193. else if (screenshot) {
  194. INFO("Taking screenshots of all modules at %gx zoom", screenshotZoom);
  195. APP->window->screenshotModules(asset::user("screenshots"), screenshotZoom);
  196. }
  197. else {
  198. INFO("Running window");
  199. APP->window->run();
  200. INFO("Stopped window");
  201. // INFO("Destroying window");
  202. // delete APP->window;
  203. // APP->window = NULL;
  204. // INFO("Re-creating window");
  205. // APP->window = new Window;
  206. // APP->window->run();
  207. }
  208. // Destroy context
  209. if (!settings::headless) {
  210. APP->patch->saveAutosave();
  211. }
  212. INFO("Destroying context");
  213. delete APP;
  214. contextSet(NULL);
  215. if (!settings::headless) {
  216. settings::save();
  217. }
  218. // Destroy environment
  219. INFO("Destroying environment");
  220. if (!settings::headless) {
  221. windowDestroy();
  222. ui::destroy();
  223. }
  224. library::destroy();
  225. midi::destroy();
  226. audio::destroy();
  227. plugin::destroy();
  228. INFO("Destroying logger");
  229. logger::destroy();
  230. return 0;
  231. }