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.

main.cpp 5.6KB

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