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.

341 lines
8.6KB

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