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.

307 lines
7.7KB

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