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.

324 lines
8.2KB

  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 <midiloopback.hpp>
  11. #include <settings.hpp>
  12. #include <engine/Engine.hpp>
  13. #include <app/common.hpp>
  14. #include <app/Scene.hpp>
  15. #include <app/Browser.hpp>
  16. #include <plugin.hpp>
  17. #include <context.hpp>
  18. #include <window/Window.hpp>
  19. #include <patch.hpp>
  20. #include <history.hpp>
  21. #include <ui/common.hpp>
  22. #include <system.hpp>
  23. #include <string.hpp>
  24. #include <library.hpp>
  25. #include <network.hpp>
  26. #include <getopt.h>
  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. #include <osdialog.h>
  33. #if defined ARCH_MAC
  34. #define GLFW_EXPOSE_NATIVE_COCOA
  35. #include <GLFW/glfw3native.h> // for glfwGetOpenedFilenames()
  36. #endif
  37. using namespace rack;
  38. static void fatalSignalHandler(int sig) {
  39. // Ignore this signal to avoid recursion.
  40. signal(sig, NULL);
  41. std::string stackTrace = system::getStackTrace();
  42. FATAL("Fatal signal %d. Stack trace:\n%s", sig, stackTrace.c_str());
  43. // Re-raise signal
  44. raise(sig);
  45. }
  46. int main(int argc, char* argv[]) {
  47. #if defined ARCH_WIN
  48. // Windows global mutex to prevent multiple instances
  49. // Handle will be closed by Windows when the process ends
  50. HANDLE instanceMutex = CreateMutexW(NULL, true, string::UTF8toUTF16(APP_NAME).c_str());
  51. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  52. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Rack is already running. Multiple Rack instances are not supported.");
  53. exit(1);
  54. }
  55. (void) instanceMutex;
  56. // Don't display "Assertion failed!" dialog message.
  57. _set_error_mode(_OUT_TO_STDERR);
  58. #endif
  59. std::string patchPath;
  60. bool screenshot = false;
  61. float screenshotZoom = 1.f;
  62. const std::string appInfo = APP_NAME + " " + APP_EDITION_NAME + " " + APP_VERSION + " " + APP_OS_NAME + " " + APP_CPU_NAME;
  63. // Parse command line arguments
  64. static const struct option longOptions[] = {
  65. {"safe", no_argument, NULL, 'a'},
  66. {"dev", no_argument, NULL, 'd'},
  67. {"headless", no_argument, NULL, 'h'},
  68. {"screenshot", required_argument, NULL, 't'},
  69. {"system", required_argument, NULL, 's'},
  70. {"user", required_argument, NULL, 'u'},
  71. {"version", no_argument, NULL, 'v'},
  72. {"help", no_argument, NULL, 256},
  73. {NULL, 0, NULL, 0}
  74. };
  75. int c;
  76. opterr = 0;
  77. while ((c = getopt_long(argc, argv, "adht:s:u:vp:", longOptions, NULL)) != -1) {
  78. switch (c) {
  79. case 'a': {
  80. settings::safeMode = true;
  81. } break;
  82. case 'd': {
  83. settings::devMode = true;
  84. } break;
  85. case 'h': {
  86. settings::headless = true;
  87. } break;
  88. case 't': {
  89. screenshot = true;
  90. std::sscanf(optarg, "%f", &screenshotZoom);
  91. } break;
  92. case 's': {
  93. asset::systemDir = optarg;
  94. } break;
  95. case 'u': {
  96. asset::userDir = optarg;
  97. } break;
  98. case 'v': {
  99. std::fprintf(stderr, "%s\n", appInfo.c_str());
  100. return 0;
  101. }
  102. case 256: { // --help
  103. std::fprintf(stderr, "%s\n", appInfo.c_str());
  104. std::fprintf(stderr, "https://vcvrack.com/manual/Installing#Command-line-usage\n");
  105. return 0;
  106. }
  107. // Mac "app translocation" passes a nonsense -psn_... flag, so -p is reserved.
  108. case 'p': break;
  109. default: break;
  110. }
  111. }
  112. if (optind < argc) {
  113. patchPath = argv[optind];
  114. }
  115. // Initialize environment
  116. system::init();
  117. asset::init();
  118. if (!settings::devMode) {
  119. logger::logPath = asset::user("log.txt");
  120. }
  121. logger::init();
  122. random::init();
  123. // Test code
  124. // exit(0);
  125. // We can now install a signal handler and log the output
  126. if (!settings::devMode) {
  127. signal(SIGABRT, fatalSignalHandler);
  128. signal(SIGFPE, fatalSignalHandler);
  129. signal(SIGILL, fatalSignalHandler);
  130. signal(SIGSEGV, fatalSignalHandler);
  131. signal(SIGTERM, fatalSignalHandler);
  132. }
  133. // Log environment
  134. INFO("%s", appInfo.c_str());
  135. INFO("%s", system::getOperatingSystemInfo().c_str());
  136. std::string argsList;
  137. for (int i = 0; i < argc; i++) {
  138. argsList += argv[i];
  139. argsList += " ";
  140. }
  141. INFO("Args: %s", argsList.c_str());
  142. if (settings::devMode)
  143. INFO("Development mode");
  144. INFO("System directory: %s", asset::systemDir.c_str());
  145. INFO("User directory: %s", asset::userDir.c_str());
  146. #if defined ARCH_MAC
  147. INFO("Bundle path: %s", asset::bundlePath.c_str());
  148. #endif
  149. INFO("System time: %s", string::formatTimeISO(system::getUnixTime()).c_str());
  150. // Load settings
  151. settings::init();
  152. try {
  153. settings::load();
  154. }
  155. catch (Exception& e) {
  156. std::string msg = e.what();
  157. msg += "\n\nReset settings to default?";
  158. if (!osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK_CANCEL, msg.c_str())) {
  159. exit(1);
  160. }
  161. }
  162. // Check existence of the system res/ directory
  163. std::string resDir = asset::system("res");
  164. if (!system::isDirectory(resDir)) {
  165. std::string message = string::f("Rack's resource directory \"%s\" does not exist. Make sure Rack is correctly installed and launched.", resDir.c_str());
  166. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, message.c_str());
  167. exit(1);
  168. }
  169. INFO("Initializing network");
  170. network::init();
  171. INFO("Initializing audio");
  172. audio::init();
  173. rtaudioInit();
  174. INFO("Initializing MIDI");
  175. midi::init();
  176. rtmidiInit();
  177. keyboard::init();
  178. gamepad::init();
  179. midiloopback::init();
  180. INFO("Initializing plugins");
  181. plugin::init();
  182. INFO("Initializing browser");
  183. app::browserInit();
  184. INFO("Initializing library");
  185. library::init();
  186. if (!settings::headless) {
  187. INFO("Initializing UI");
  188. ui::init();
  189. INFO("Initializing window");
  190. window::init();
  191. }
  192. // Initialize context
  193. contextSet(new Context);
  194. INFO("Creating MIDI loopback");
  195. APP->midiLoopbackContext = new midiloopback::Context;
  196. INFO("Creating engine");
  197. APP->engine = new engine::Engine;
  198. INFO("Creating history state");
  199. APP->history = new history::State;
  200. INFO("Creating event state");
  201. APP->event = new widget::EventState;
  202. INFO("Creating scene");
  203. APP->scene = new app::Scene;
  204. APP->event->rootWidget = APP->scene;
  205. INFO("Creating patch manager");
  206. APP->patch = new patch::Manager;
  207. if (!settings::headless) {
  208. INFO("Creating window");
  209. APP->window = new window::Window;
  210. }
  211. // On Mac, use a hacked-in GLFW addition to get the launched path.
  212. #if defined ARCH_MAC
  213. // For some reason, launching from the command line sets glfwGetOpenedFilenames(), so make sure we're running the app bundle.
  214. if (asset::bundlePath != "") {
  215. const char* const* openedFilenames = glfwGetOpenedFilenames();
  216. if (openedFilenames && openedFilenames[0]) {
  217. patchPath = openedFilenames[0];
  218. }
  219. }
  220. #endif
  221. // Initialize patch
  222. 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?")) {
  223. // Do nothing, which leaves a blank patch
  224. }
  225. else {
  226. APP->patch->launch(patchPath);
  227. }
  228. APP->engine->startFallbackThread();
  229. // Run context
  230. if (settings::headless) {
  231. printf("Press enter to exit.\n");
  232. getchar();
  233. }
  234. else if (screenshot) {
  235. INFO("Taking screenshots of all modules at %gx zoom", screenshotZoom);
  236. APP->window->screenshotModules(asset::user("screenshots"), screenshotZoom);
  237. }
  238. else {
  239. INFO("Running window");
  240. APP->window->run();
  241. INFO("Stopped window");
  242. // INFO("Destroying window");
  243. // delete APP->window;
  244. // APP->window = NULL;
  245. // INFO("Re-creating window");
  246. // APP->window = new window::Window;
  247. // APP->window->run();
  248. }
  249. // Destroy context
  250. INFO("Deleting context");
  251. delete APP;
  252. contextSet(NULL);
  253. if (!settings::headless) {
  254. settings::save();
  255. }
  256. // Destroy environment
  257. if (!settings::headless) {
  258. INFO("Destroying window");
  259. window::destroy();
  260. INFO("Destroying UI");
  261. ui::destroy();
  262. }
  263. INFO("Destroying library");
  264. library::destroy();
  265. INFO("Destroying MIDI");
  266. midi::destroy();
  267. INFO("Destroying audio");
  268. audio::destroy();
  269. INFO("Destroying plugins");
  270. plugin::destroy();
  271. INFO("Destroying network");
  272. network::destroy();
  273. settings::destroy();
  274. INFO("Destroying logger");
  275. logger::destroy();
  276. return 0;
  277. }
  278. #ifdef UNICODE
  279. /** UTF-16 to UTF-8 wrapper for Windows with unicode */
  280. int wmain(int argc, wchar_t* argvU16[]) {
  281. // Initialize char* array with string-owned buffers
  282. std::string argvStr[argc];
  283. const char* argvU8[argc + 1];
  284. for (int i = 0; i < argc; i++) {
  285. argvStr[i] = string::UTF16toUTF8(argvU16[i]);
  286. argvU8[i] = argvStr[i].c_str();
  287. }
  288. argvU8[argc] = NULL;
  289. return main(argc, (char**) argvU8);
  290. }
  291. #endif