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