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 7.1KB

8 years ago
3 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 <settings.hpp>
  11. #include <engine/Engine.hpp>
  12. #include <app/common.hpp>
  13. #include <app/Scene.hpp>
  14. #include <app/Browser.hpp>
  15. #include <plugin.hpp>
  16. #include <context.hpp>
  17. #include <window/Window.hpp>
  18. #include <patch.hpp>
  19. #include <history.hpp>
  20. #include <ui/common.hpp>
  21. #include <system.hpp>
  22. #include <string.hpp>
  23. #include <library.hpp>
  24. #include <network.hpp>
  25. #include <discord.hpp>
  26. #include <osdialog.h>
  27. #include <thread>
  28. #include <unistd.h> // for getopt
  29. #include <signal.h> // for signal
  30. #if defined ARCH_WIN
  31. #include <windows.h> // for CreateMutex
  32. #endif
  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. // Ignore abort() since we call it below.
  42. signal(SIGABRT, NULL);
  43. FATAL("Fatal signal %d. Stack trace:\n%s", sig, system::getStackTrace().c_str());
  44. // This might fail because we might not be in the main thread.
  45. // But oh well, we're crashing anyway.
  46. std::string text = APP_NAME + " has crashed. See " + logger::logPath + " for details.";
  47. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, text.c_str());
  48. abort();
  49. }
  50. int main(int argc, char* argv[]) {
  51. #if defined ARCH_WIN
  52. // Windows global mutex to prevent multiple instances
  53. // Handle will be closed by Windows when the process ends
  54. HANDLE instanceMutex = CreateMutexW(NULL, true, string::UTF8toUTF16(APP_NAME).c_str());
  55. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  56. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Rack is already running. Multiple Rack instances are not supported.");
  57. exit(1);
  58. }
  59. (void) instanceMutex;
  60. // Don't display "Assertion failed!" dialog message.
  61. _set_error_mode(_OUT_TO_STDERR);
  62. #endif
  63. std::string patchPath;
  64. bool screenshot = false;
  65. float screenshotZoom = 1.f;
  66. // Parse command line arguments
  67. int c;
  68. opterr = 0;
  69. while ((c = getopt(argc, argv, "dht:s:u:p:")) != -1) {
  70. switch (c) {
  71. case 'd': {
  72. settings::devMode = true;
  73. } break;
  74. case 'h': {
  75. settings::headless = true;
  76. } break;
  77. case 't': {
  78. screenshot = true;
  79. std::sscanf(optarg, "%f", &screenshotZoom);
  80. } break;
  81. case 's': {
  82. asset::systemDir = optarg;
  83. } break;
  84. case 'u': {
  85. asset::userDir = optarg;
  86. } break;
  87. // Mac "app translocation" passes a nonsense -psn_... flag, so -p is reserved.
  88. case 'p': break;
  89. default: break;
  90. }
  91. }
  92. if (optind < argc) {
  93. patchPath = argv[optind];
  94. }
  95. // Initialize environment
  96. system::init();
  97. asset::init();
  98. if (!settings::devMode) {
  99. logger::logPath = asset::user("log.txt");
  100. }
  101. logger::init();
  102. random::init();
  103. // Test code
  104. // exit(0);
  105. // We can now install a signal handler and log the output
  106. if (!settings::devMode) {
  107. signal(SIGABRT, fatalSignalHandler);
  108. signal(SIGFPE, fatalSignalHandler);
  109. signal(SIGILL, fatalSignalHandler);
  110. signal(SIGSEGV, fatalSignalHandler);
  111. signal(SIGTERM, fatalSignalHandler);
  112. }
  113. // Log environment
  114. INFO("%s %s v%s", APP_NAME.c_str(), APP_EDITION_NAME.c_str(), APP_VERSION.c_str());
  115. INFO("%s", system::getOperatingSystemInfo().c_str());
  116. std::string argsList;
  117. for (int i = 0; i < argc; i++) {
  118. argsList += argv[i];
  119. argsList += " ";
  120. }
  121. INFO("Args: %s", argsList.c_str());
  122. if (settings::devMode)
  123. INFO("Development mode");
  124. INFO("System directory: %s", asset::systemDir.c_str());
  125. INFO("User directory: %s", asset::userDir.c_str());
  126. #if defined ARCH_MAC
  127. INFO("Bundle path: %s", asset::bundlePath.c_str());
  128. #endif
  129. INFO("System time: %s", string::formatTimeISO(system::getUnixTime()).c_str());
  130. // Load settings
  131. settings::init();
  132. try {
  133. settings::load();
  134. }
  135. catch (Exception& e) {
  136. std::string msg = e.what();
  137. msg += "\n\nReset settings to default?";
  138. if (!osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK_CANCEL, msg.c_str())) {
  139. exit(1);
  140. }
  141. }
  142. // Check existence of the system res/ directory
  143. std::string resDir = asset::system("res");
  144. if (!system::isDirectory(resDir)) {
  145. std::string message = string::f("Rack's resource directory \"%s\" does not exist. Make sure Rack is correctly installed and launched.", resDir.c_str());
  146. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, message.c_str());
  147. exit(1);
  148. }
  149. INFO("Initializing environment");
  150. network::init();
  151. audio::init();
  152. rtaudioInit();
  153. midi::init();
  154. rtmidiInit();
  155. keyboard::init();
  156. gamepad::init();
  157. plugin::init();
  158. app::browserInit();
  159. library::init();
  160. discord::init();
  161. if (!settings::headless) {
  162. ui::init();
  163. window::init();
  164. }
  165. // Initialize context
  166. INFO("Initializing context");
  167. contextSet(new Context);
  168. APP->engine = new engine::Engine;
  169. APP->history = new history::State;
  170. APP->event = new widget::EventState;
  171. APP->scene = new app::Scene;
  172. APP->event->rootWidget = APP->scene;
  173. APP->patch = new patch::Manager;
  174. if (!settings::headless) {
  175. APP->window = new window::Window;
  176. }
  177. // On Mac, use a hacked-in GLFW addition to get the launched path.
  178. #if defined ARCH_MAC
  179. // For some reason, launching from the command line sets glfwGetOpenedFilenames(), so make sure we're running the app bundle.
  180. if (asset::bundlePath != "") {
  181. // const char* const* openedFilenames = glfwGetOpenedFilenames();
  182. // if (openedFilenames && openedFilenames[0]) {
  183. // patchPath = openedFilenames[0];
  184. // }
  185. }
  186. #endif
  187. // Initialize patch
  188. 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?")) {
  189. // Do nothing, which leaves a blank patch
  190. }
  191. else {
  192. APP->patch->launch(patchPath);
  193. }
  194. APP->engine->startFallbackThread();
  195. // Run context
  196. if (settings::headless) {
  197. printf("Press enter to exit.\n");
  198. getchar();
  199. }
  200. else if (screenshot) {
  201. INFO("Taking screenshots of all modules at %gx zoom", screenshotZoom);
  202. APP->window->screenshotModules(asset::user("screenshots"), screenshotZoom);
  203. }
  204. else {
  205. INFO("Running window");
  206. APP->window->run();
  207. INFO("Stopped window");
  208. // INFO("Destroying window");
  209. // delete APP->window;
  210. // APP->window = NULL;
  211. // INFO("Re-creating window");
  212. // APP->window = new window::Window;
  213. // APP->window->run();
  214. }
  215. // Destroy context
  216. INFO("Destroying context");
  217. delete APP;
  218. contextSet(NULL);
  219. if (!settings::headless) {
  220. settings::save();
  221. }
  222. // Destroy environment
  223. INFO("Destroying environment");
  224. if (!settings::headless) {
  225. window::destroy();
  226. ui::destroy();
  227. }
  228. discord::destroy();
  229. library::destroy();
  230. midi::destroy();
  231. audio::destroy();
  232. plugin::destroy();
  233. network::destroy();
  234. INFO("Destroying logger");
  235. logger::destroy();
  236. return 0;
  237. }
  238. #ifdef UNICODE
  239. /** UTF-16 to UTF-8 wrapper for Windows with unicode */
  240. int wmain(int argc, wchar_t* argvU16[]) {
  241. // Initialize char* array with string-owned buffers
  242. std::string argvStr[argc];
  243. const char* argvU8[argc + 1];
  244. for (int i = 0; i < argc; i++) {
  245. argvStr[i] = string::UTF16toUTF8(argvU16[i]);
  246. argvU8[i] = argvStr[i].c_str();
  247. }
  248. argvU8[argc] = NULL;
  249. return main(argc, (char**) argvU8);
  250. }
  251. #endif