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.

325 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. system::resetFpuFlags();
  118. asset::init();
  119. if (!settings::devMode) {
  120. logger::logPath = asset::user("log.txt");
  121. }
  122. logger::init();
  123. random::init();
  124. // Test code
  125. // exit(0);
  126. // We can now install a signal handler and log the output
  127. if (!settings::devMode) {
  128. signal(SIGABRT, fatalSignalHandler);
  129. signal(SIGFPE, fatalSignalHandler);
  130. signal(SIGILL, fatalSignalHandler);
  131. signal(SIGSEGV, fatalSignalHandler);
  132. signal(SIGTERM, fatalSignalHandler);
  133. }
  134. // Log environment
  135. INFO("%s", appInfo.c_str());
  136. INFO("%s", system::getOperatingSystemInfo().c_str());
  137. std::string argsList;
  138. for (int i = 0; i < argc; i++) {
  139. argsList += argv[i];
  140. argsList += " ";
  141. }
  142. INFO("Args: %s", argsList.c_str());
  143. if (settings::devMode)
  144. INFO("Development mode");
  145. INFO("System directory: %s", asset::systemDir.c_str());
  146. INFO("User directory: %s", asset::userDir.c_str());
  147. #if defined ARCH_MAC
  148. INFO("Bundle path: %s", asset::bundlePath.c_str());
  149. #endif
  150. INFO("System time: %s", string::formatTimeISO(system::getUnixTime()).c_str());
  151. // Load settings
  152. settings::init();
  153. try {
  154. settings::load();
  155. }
  156. catch (Exception& e) {
  157. std::string msg = e.what();
  158. msg += "\n\nReset settings to default?";
  159. if (!osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK_CANCEL, msg.c_str())) {
  160. exit(1);
  161. }
  162. }
  163. // Check existence of the system res/ directory
  164. std::string resDir = asset::system("res");
  165. if (!system::isDirectory(resDir)) {
  166. std::string message = string::f("Rack's resource directory \"%s\" does not exist. Make sure Rack is correctly installed and launched.", resDir.c_str());
  167. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, message.c_str());
  168. exit(1);
  169. }
  170. INFO("Initializing network");
  171. network::init();
  172. INFO("Initializing audio");
  173. audio::init();
  174. rtaudioInit();
  175. INFO("Initializing MIDI");
  176. midi::init();
  177. rtmidiInit();
  178. keyboard::init();
  179. gamepad::init();
  180. midiloopback::init();
  181. INFO("Initializing plugins");
  182. plugin::init();
  183. INFO("Initializing browser");
  184. app::browserInit();
  185. INFO("Initializing library");
  186. library::init();
  187. if (!settings::headless) {
  188. INFO("Initializing UI");
  189. ui::init();
  190. INFO("Initializing window");
  191. window::init();
  192. }
  193. // Initialize context
  194. contextSet(new Context);
  195. INFO("Creating MIDI loopback");
  196. APP->midiLoopbackContext = new midiloopback::Context;
  197. INFO("Creating engine");
  198. APP->engine = new engine::Engine;
  199. INFO("Creating history state");
  200. APP->history = new history::State;
  201. INFO("Creating event state");
  202. APP->event = new widget::EventState;
  203. INFO("Creating scene");
  204. APP->scene = new app::Scene;
  205. APP->event->rootWidget = APP->scene;
  206. INFO("Creating patch manager");
  207. APP->patch = new patch::Manager;
  208. if (!settings::headless) {
  209. INFO("Creating window");
  210. APP->window = new window::Window;
  211. }
  212. // On Mac, use a hacked-in GLFW addition to get the launched path.
  213. #if defined ARCH_MAC
  214. // For some reason, launching from the command line sets glfwGetOpenedFilenames(), so make sure we're running the app bundle.
  215. if (asset::bundlePath != "") {
  216. const char* const* openedFilenames = glfwGetOpenedFilenames();
  217. if (openedFilenames && openedFilenames[0]) {
  218. patchPath = openedFilenames[0];
  219. }
  220. }
  221. #endif
  222. // Initialize patch
  223. 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?")) {
  224. // Do nothing, which leaves a blank patch
  225. }
  226. else {
  227. APP->patch->launch(patchPath);
  228. }
  229. APP->engine->startFallbackThread();
  230. // Run context
  231. if (settings::headless) {
  232. printf("Press enter to exit.\n");
  233. getchar();
  234. }
  235. else if (screenshot) {
  236. INFO("Taking screenshots of all modules at %gx zoom", screenshotZoom);
  237. APP->window->screenshotModules(asset::user("screenshots"), screenshotZoom);
  238. }
  239. else {
  240. INFO("Running window");
  241. APP->window->run();
  242. INFO("Stopped window");
  243. // INFO("Destroying window");
  244. // delete APP->window;
  245. // APP->window = NULL;
  246. // INFO("Re-creating window");
  247. // APP->window = new window::Window;
  248. // APP->window->run();
  249. }
  250. // Destroy context
  251. INFO("Deleting context");
  252. delete APP;
  253. contextSet(NULL);
  254. if (!settings::headless) {
  255. settings::save();
  256. }
  257. // Destroy environment
  258. if (!settings::headless) {
  259. INFO("Destroying window");
  260. window::destroy();
  261. INFO("Destroying UI");
  262. ui::destroy();
  263. }
  264. INFO("Destroying library");
  265. library::destroy();
  266. INFO("Destroying MIDI");
  267. midi::destroy();
  268. INFO("Destroying audio");
  269. audio::destroy();
  270. INFO("Destroying plugins");
  271. plugin::destroy();
  272. INFO("Destroying network");
  273. network::destroy();
  274. settings::destroy();
  275. INFO("Destroying logger");
  276. logger::destroy();
  277. return 0;
  278. }
  279. #ifdef UNICODE
  280. /** UTF-16 to UTF-8 wrapper for Windows with unicode */
  281. int wmain(int argc, wchar_t* argvU16[]) {
  282. // Initialize char* array with string-owned buffers
  283. std::string argvStr[argc];
  284. const char* argvU8[argc + 1];
  285. for (int i = 0; i < argc; i++) {
  286. argvStr[i] = string::UTF16toUTF8(argvU16[i]);
  287. argvU8[i] = argvStr[i].c_str();
  288. }
  289. argvU8[argc] = NULL;
  290. return main(argc, (char**) argvU8);
  291. }
  292. #endif