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.

332 lines
8.7KB

  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, "VCV 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("VCV 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. #if defined ARCH_MAC
  176. if (rtaudioIsMicrophoneBlocked()) {
  177. std::string msg = "VCV Rack cannot access audio input because Microphone permission is blocked.";
  178. msg += "\n\nGive permission to Rack by opening Apple's System Settings and enabling Privacy & Security > Microphone > " + APP_NAME + " " + APP_VERSION_MAJOR + " " + APP_EDITION_NAME + ".";
  179. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, msg.c_str());
  180. }
  181. #endif
  182. INFO("Initializing MIDI");
  183. midi::init();
  184. rtmidiInit();
  185. keyboard::init();
  186. gamepad::init();
  187. midiloopback::init();
  188. INFO("Initializing plugins");
  189. plugin::init();
  190. INFO("Initializing browser");
  191. app::browserInit();
  192. INFO("Initializing library");
  193. library::init();
  194. if (!settings::headless) {
  195. INFO("Initializing UI");
  196. ui::init();
  197. INFO("Initializing window");
  198. window::init();
  199. }
  200. // Initialize context
  201. contextSet(new Context);
  202. INFO("Creating MIDI loopback");
  203. APP->midiLoopbackContext = new midiloopback::Context;
  204. INFO("Creating engine");
  205. APP->engine = new engine::Engine;
  206. INFO("Creating history state");
  207. APP->history = new history::State;
  208. INFO("Creating event state");
  209. APP->event = new widget::EventState;
  210. INFO("Creating scene");
  211. APP->scene = new app::Scene;
  212. APP->event->rootWidget = APP->scene;
  213. INFO("Creating patch manager");
  214. APP->patch = new patch::Manager;
  215. if (!settings::headless) {
  216. INFO("Creating window");
  217. APP->window = new window::Window;
  218. }
  219. // On Mac, use a hacked-in GLFW addition to get the launched path.
  220. #if defined ARCH_MAC
  221. // For some reason, launching from the command line sets glfwGetOpenedFilenames(), so make sure we're running the app bundle.
  222. if (asset::bundlePath != "") {
  223. const char* const* openedFilenames = glfwGetOpenedFilenames();
  224. if (openedFilenames && openedFilenames[0]) {
  225. patchPath = openedFilenames[0];
  226. }
  227. }
  228. #endif
  229. // Initialize patch
  230. if (logger::wasTruncated() && osdialog_message(OSDIALOG_INFO, OSDIALOG_YES_NO, "VCV Rack crashed during the last session, possibly due to a buggy module in your patch. Clear your patch and start over?")) {
  231. // Do nothing, which leaves a blank patch
  232. }
  233. else {
  234. APP->patch->launch(patchPath);
  235. }
  236. APP->engine->startFallbackThread();
  237. // Run context
  238. if (settings::headless) {
  239. printf("Press enter to exit.\n");
  240. getchar();
  241. }
  242. else if (screenshot) {
  243. INFO("Taking screenshots of all modules at %gx zoom", screenshotZoom);
  244. APP->window->screenshotModules(asset::user("screenshots"), screenshotZoom);
  245. }
  246. else {
  247. INFO("Running window");
  248. APP->window->run();
  249. INFO("Stopped window");
  250. // INFO("Destroying window");
  251. // delete APP->window;
  252. // APP->window = NULL;
  253. // INFO("Re-creating window");
  254. // APP->window = new window::Window;
  255. // APP->window->run();
  256. }
  257. // Destroy context
  258. INFO("Deleting context");
  259. delete APP;
  260. contextSet(NULL);
  261. if (!settings::headless) {
  262. settings::save();
  263. }
  264. // Destroy environment
  265. if (!settings::headless) {
  266. INFO("Destroying window");
  267. window::destroy();
  268. INFO("Destroying UI");
  269. ui::destroy();
  270. }
  271. INFO("Destroying library");
  272. library::destroy();
  273. INFO("Destroying MIDI");
  274. midi::destroy();
  275. INFO("Destroying audio");
  276. audio::destroy();
  277. INFO("Destroying plugins");
  278. plugin::destroy();
  279. INFO("Destroying network");
  280. network::destroy();
  281. settings::destroy();
  282. INFO("Destroying logger");
  283. logger::destroy();
  284. return 0;
  285. }
  286. #ifdef UNICODE
  287. /** UTF-16 to UTF-8 wrapper for Windows with unicode */
  288. int wmain(int argc, wchar_t* argvU16[]) {
  289. // Initialize char* array with string-owned buffers
  290. std::string argvStr[argc];
  291. const char* argvU8[argc + 1];
  292. for (int i = 0; i < argc; i++) {
  293. argvStr[i] = string::UTF16toUTF8(argvU16[i]);
  294. argvU8[i] = argvStr[i].c_str();
  295. }
  296. argvU8[argc] = NULL;
  297. return main(argc, (char**) argvU8);
  298. }
  299. #endif