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.

336 lines
8.9KB

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