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.

281 lines
7.0KB

  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/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 <discord.hpp>
  25. #include <osdialog.h>
  26. #include <thread>
  27. #include <unistd.h> // for getopt
  28. #include <signal.h> // for signal
  29. #if defined ARCH_WIN
  30. #include <windows.h> // for CreateMutex
  31. #endif
  32. #if defined ARCH_MAC
  33. #define GLFW_EXPOSE_NATIVE_COCOA
  34. #include <GLFW/glfw3native.h> // for glfwGetOpenedFilenames()
  35. #endif
  36. using namespace rack;
  37. static void fatalSignalHandler(int sig) {
  38. // Ignore this signal to avoid recursion.
  39. signal(sig, NULL);
  40. // Ignore abort() since we call it below.
  41. signal(SIGABRT, NULL);
  42. FATAL("Fatal signal %d. Stack trace:\n%s", sig, system::getStackTrace().c_str());
  43. // This might fail because we might not be in the main thread.
  44. // But oh well, we're crashing anyway.
  45. std::string text = APP_NAME + " has crashed. See " + logger::logPath + " for details.";
  46. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, text.c_str());
  47. abort();
  48. }
  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. if (!settings::devMode) {
  98. logger::logPath = asset::user("log.txt");
  99. }
  100. logger::init();
  101. random::init();
  102. // Test code
  103. // exit(0);
  104. // We can now install a signal handler and log the output
  105. if (!settings::devMode) {
  106. signal(SIGABRT, fatalSignalHandler);
  107. signal(SIGFPE, fatalSignalHandler);
  108. signal(SIGILL, fatalSignalHandler);
  109. signal(SIGSEGV, fatalSignalHandler);
  110. signal(SIGTERM, fatalSignalHandler);
  111. }
  112. // Log environment
  113. INFO("%s %s v%s", APP_NAME.c_str(), APP_EDITION_NAME.c_str(), APP_VERSION.c_str());
  114. INFO("%s", system::getOperatingSystemInfo().c_str());
  115. std::string argsList;
  116. for (int i = 0; i < argc; i++) {
  117. argsList += argv[i];
  118. argsList += " ";
  119. }
  120. INFO("Args: %s", argsList.c_str());
  121. if (settings::devMode)
  122. INFO("Development mode");
  123. INFO("System directory: %s", asset::systemDir.c_str());
  124. INFO("User directory: %s", asset::userDir.c_str());
  125. #if defined ARCH_MAC
  126. INFO("Bundle path: %s", asset::bundlePath.c_str());
  127. #endif
  128. INFO("System time: %s", string::formatTimeISO(system::getUnixTime()).c_str());
  129. // Load settings
  130. settings::init();
  131. try {
  132. settings::load();
  133. }
  134. catch (Exception& e) {
  135. std::string msg = e.what();
  136. msg += "\n\nReset settings to default?";
  137. if (!osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK_CANCEL, msg.c_str())) {
  138. exit(1);
  139. }
  140. }
  141. // Check existence of the system res/ directory
  142. std::string resDir = asset::system("res");
  143. if (!system::isDirectory(resDir)) {
  144. std::string message = string::f("Rack's resource directory \"%s\" does not exist. Make sure Rack is correctly installed and launched.", resDir.c_str());
  145. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, message.c_str());
  146. exit(1);
  147. }
  148. INFO("Initializing environment");
  149. network::init();
  150. audio::init();
  151. rtaudioInit();
  152. midi::init();
  153. rtmidiInit();
  154. keyboard::init();
  155. gamepad::init();
  156. plugin::init();
  157. library::init();
  158. discord::init();
  159. if (!settings::headless) {
  160. ui::init();
  161. window::init();
  162. }
  163. // Initialize context
  164. INFO("Initializing context");
  165. contextSet(new Context);
  166. APP->engine = new engine::Engine;
  167. APP->history = new history::State;
  168. APP->event = new widget::EventState;
  169. APP->scene = new app::Scene;
  170. APP->event->rootWidget = APP->scene;
  171. APP->patch = new patch::Manager;
  172. if (!settings::headless) {
  173. APP->window = new window::Window;
  174. }
  175. // On Mac, use a hacked-in GLFW addition to get the launched path.
  176. #if defined ARCH_MAC
  177. // For some reason, launching from the command line sets glfwGetOpenedFilenames(), so make sure we're running the app bundle.
  178. if (asset::bundlePath != "") {
  179. // const char* const* openedFilenames = glfwGetOpenedFilenames();
  180. // if (openedFilenames && openedFilenames[0]) {
  181. // patchPath = openedFilenames[0];
  182. // }
  183. }
  184. #endif
  185. // Initialize patch
  186. if (logger::wasTruncated() && 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?")) {
  187. // Do nothing, which leaves a blank patch
  188. }
  189. else {
  190. APP->patch->launch(patchPath);
  191. }
  192. APP->engine->startFallbackThread();
  193. // Run context
  194. if (settings::headless) {
  195. printf("Press enter to exit.\n");
  196. getchar();
  197. }
  198. else if (screenshot) {
  199. INFO("Taking screenshots of all modules at %gx zoom", screenshotZoom);
  200. APP->window->screenshotModules(asset::user("screenshots"), screenshotZoom);
  201. }
  202. else {
  203. INFO("Running window");
  204. APP->window->run();
  205. INFO("Stopped window");
  206. // INFO("Destroying window");
  207. // delete APP->window;
  208. // APP->window = NULL;
  209. // INFO("Re-creating window");
  210. // APP->window = new window::Window;
  211. // APP->window->run();
  212. }
  213. // Destroy context
  214. INFO("Destroying context");
  215. delete APP;
  216. contextSet(NULL);
  217. if (!settings::headless) {
  218. settings::save();
  219. }
  220. // Destroy environment
  221. INFO("Destroying environment");
  222. if (!settings::headless) {
  223. window::destroy();
  224. ui::destroy();
  225. }
  226. discord::destroy();
  227. library::destroy();
  228. midi::destroy();
  229. audio::destroy();
  230. plugin::destroy();
  231. INFO("Destroying logger");
  232. logger::destroy();
  233. return 0;
  234. }
  235. #ifdef UNICODE
  236. /** UTF-16 to UTF-8 wrapper for Windows with unicode */
  237. int wmain(int argc, wchar_t* argvU16[]) {
  238. // Initialize char* array with string-owned buffers
  239. std::string argvStr[argc];
  240. const char* argvU8[argc + 1];
  241. for (int i = 0; i < argc; i++) {
  242. argvStr[i] = string::UTF16toUTF8(argvU16[i]);
  243. argvU8[i] = argvStr[i].c_str();
  244. }
  245. argvU8[argc] = NULL;
  246. return main(argc, (char**) argvU8);
  247. }
  248. #endif