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.0KB

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