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.

318 lines
8.0KB

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