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