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.

298 lines
7.3KB

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