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.

standalone.cpp 8.9KB

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