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.

276 lines
6.8KB

  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. int main(int argc, char* argv[]) {
  49. #if defined ARCH_WIN
  50. // Windows global mutex to prevent multiple instances
  51. // Handle will be closed by Windows when the process ends
  52. HANDLE instanceMutex = CreateMutexW(NULL, true, string::UTF8toUTF16(APP_NAME).c_str());
  53. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  54. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Rack is already running. Multiple Rack instances are not supported.");
  55. exit(1);
  56. }
  57. (void) instanceMutex;
  58. // Don't display "Assertion failed!" dialog message.
  59. _set_error_mode(_OUT_TO_STDERR);
  60. #endif
  61. std::string patchPath;
  62. bool screenshot = false;
  63. float screenshotZoom = 1.f;
  64. // Parse command line arguments
  65. int c;
  66. opterr = 0;
  67. while ((c = getopt(argc, argv, "dht:s:u:p:")) != -1) {
  68. switch (c) {
  69. case 'd': {
  70. settings::devMode = true;
  71. } break;
  72. case 'h': {
  73. settings::headless = true;
  74. } break;
  75. case 't': {
  76. screenshot = true;
  77. std::sscanf(optarg, "%f", &screenshotZoom);
  78. } break;
  79. case 's': {
  80. asset::systemDir = optarg;
  81. } break;
  82. case 'u': {
  83. asset::userDir = optarg;
  84. } break;
  85. // Mac "app translocation" passes a nonsense -psn_... flag, so -p is reserved.
  86. case 'p': break;
  87. default: break;
  88. }
  89. }
  90. if (optind < argc) {
  91. patchPath = argv[optind];
  92. }
  93. // Initialize environment
  94. system::init();
  95. asset::init();
  96. bool loggerWasTruncated = logger::isTruncated();
  97. logger::init();
  98. random::init();
  99. // Test code
  100. // exit(0);
  101. // We can now install a signal handler and log the output
  102. if (!settings::devMode) {
  103. signal(SIGABRT, fatalSignalHandler);
  104. signal(SIGFPE, fatalSignalHandler);
  105. signal(SIGILL, fatalSignalHandler);
  106. signal(SIGSEGV, fatalSignalHandler);
  107. signal(SIGTERM, fatalSignalHandler);
  108. }
  109. // Log environment
  110. INFO("%s %s v%s", APP_NAME.c_str(), APP_VARIANT.c_str(), APP_VERSION.c_str());
  111. INFO("%s", system::getOperatingSystemInfo().c_str());
  112. std::string argsList;
  113. for (int i = 0; i < argc; i++) {
  114. argsList += argv[i];
  115. argsList += " ";
  116. }
  117. INFO("Args: %s", argsList.c_str());
  118. if (settings::devMode)
  119. INFO("Development mode");
  120. INFO("System directory: %s", asset::systemDir.c_str());
  121. INFO("User directory: %s", asset::userDir.c_str());
  122. #if defined ARCH_MAC
  123. INFO("Bundle path: %s", asset::bundlePath.c_str());
  124. #endif
  125. // Load settings
  126. settings::init();
  127. try {
  128. settings::load();
  129. }
  130. catch (Exception& e) {
  131. std::string msg = e.what();
  132. msg += "\n\nReset settings to default?";
  133. if (!osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK_CANCEL, msg.c_str())) {
  134. exit(1);
  135. }
  136. }
  137. // Check existence of the system res/ directory
  138. std::string resDir = asset::system("res");
  139. if (!system::isDirectory(resDir)) {
  140. std::string message = string::f("Rack's resource directory \"%s\" does not exist. Make sure Rack is correctly installed and launched.", resDir.c_str());
  141. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, message.c_str());
  142. exit(1);
  143. }
  144. INFO("Initializing environment");
  145. network::init();
  146. audio::init();
  147. rtaudioInit();
  148. midi::init();
  149. rtmidiInit();
  150. keyboard::init();
  151. gamepad::init();
  152. plugin::init();
  153. library::init();
  154. if (!settings::headless) {
  155. ui::init();
  156. windowInit();
  157. }
  158. // Initialize context
  159. INFO("Initializing context");
  160. contextSet(new Context);
  161. APP->engine = new engine::Engine;
  162. APP->history = new history::State;
  163. APP->event = new widget::EventState;
  164. APP->scene = new app::Scene;
  165. APP->event->rootWidget = APP->scene;
  166. APP->patch = new PatchManager;
  167. if (!settings::headless) {
  168. APP->window = new Window;
  169. }
  170. // On Mac, use a hacked-in GLFW addition to get the launched path.
  171. #if defined ARCH_MAC
  172. // For some reason, launching from the command line sets glfwGetOpenedFilenames(), so make sure we're running the app bundle.
  173. if (asset::bundlePath != "") {
  174. // const char* const* openedFilenames = glfwGetOpenedFilenames();
  175. // if (openedFilenames && openedFilenames[0]) {
  176. // patchPath = openedFilenames[0];
  177. // }
  178. }
  179. #endif
  180. // Initialize patch
  181. 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?")) {
  182. // Do nothing
  183. }
  184. else {
  185. APP->patch->launch(patchPath);
  186. }
  187. // Run context
  188. if (settings::headless) {
  189. printf("Press enter to exit.\n");
  190. getchar();
  191. }
  192. else if (screenshot) {
  193. INFO("Taking screenshots of all modules at %gx zoom", screenshotZoom);
  194. APP->window->screenshotModules(asset::user("screenshots"), screenshotZoom);
  195. }
  196. else {
  197. INFO("Running window");
  198. APP->window->run();
  199. INFO("Stopped window");
  200. // INFO("Destroying window");
  201. // delete APP->window;
  202. // APP->window = NULL;
  203. // INFO("Re-creating window");
  204. // APP->window = new Window;
  205. // APP->window->run();
  206. }
  207. // Destroy context
  208. if (!settings::headless) {
  209. APP->patch->saveAutosave();
  210. }
  211. INFO("Destroying context");
  212. delete APP;
  213. contextSet(NULL);
  214. if (!settings::headless) {
  215. settings::save();
  216. }
  217. // Destroy environment
  218. INFO("Destroying environment");
  219. if (!settings::headless) {
  220. windowDestroy();
  221. ui::destroy();
  222. }
  223. library::destroy();
  224. midi::destroy();
  225. audio::destroy();
  226. plugin::destroy();
  227. INFO("Destroying logger");
  228. logger::destroy();
  229. return 0;
  230. }
  231. #ifdef UNICODE
  232. /** UTF-16 to UTF-8 wrapper for Windows with unicode */
  233. int wmain(int argc, wchar_t* argvU16[]) {
  234. // Initialize char* array with string-owned buffers
  235. std::string argvStr[argc];
  236. const char* argvU8[argc + 1];
  237. for (int i = 0; i < argc; i++) {
  238. argvStr[i] = string::UTF16toUTF8(argvU16[i]);
  239. argvU8[i] = argvStr[i].c_str();
  240. }
  241. argvU8[argc] = NULL;
  242. return main(argc, (char**) argvU8);
  243. }
  244. #endif