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.

302 lines
7.5KB

  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 <midiloopback.hpp>
  11. #include <settings.hpp>
  12. #include <engine/Engine.hpp>
  13. #include <app/common.hpp>
  14. #include <app/Scene.hpp>
  15. #include <app/Browser.hpp>
  16. #include <plugin.hpp>
  17. #include <context.hpp>
  18. #include <window/Window.hpp>
  19. #include <patch.hpp>
  20. #include <history.hpp>
  21. #include <ui/common.hpp>
  22. #include <system.hpp>
  23. #include <string.hpp>
  24. #include <library.hpp>
  25. #include <network.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, "adht:s:u:p:")) != -1) {
  66. switch (c) {
  67. case 'a': {
  68. settings::safeMode = true;
  69. } break;
  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 %s", APP_NAME.c_str(), APP_EDITION_NAME.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 network");
  149. network::init();
  150. INFO("Initializing audio");
  151. audio::init();
  152. rtaudioInit();
  153. INFO("Initializing MIDI");
  154. midi::init();
  155. rtmidiInit();
  156. keyboard::init();
  157. gamepad::init();
  158. midiloopback::init();
  159. INFO("Initializing plugins");
  160. plugin::init();
  161. INFO("Initializing browser");
  162. app::browserInit();
  163. INFO("Initializing library");
  164. library::init();
  165. if (!settings::headless) {
  166. INFO("Initializing UI");
  167. ui::init();
  168. INFO("Initializing window");
  169. window::init();
  170. }
  171. // Initialize context
  172. contextSet(new Context);
  173. INFO("Creating MIDI loopback");
  174. APP->midiLoopbackContext = new midiloopback::Context;
  175. INFO("Creating engine");
  176. APP->engine = new engine::Engine;
  177. INFO("Creating history state");
  178. APP->history = new history::State;
  179. INFO("Creating event state");
  180. APP->event = new widget::EventState;
  181. INFO("Creating scene");
  182. APP->scene = new app::Scene;
  183. APP->event->rootWidget = APP->scene;
  184. INFO("Creating patch manager");
  185. APP->patch = new patch::Manager;
  186. if (!settings::headless) {
  187. INFO("Creating window");
  188. APP->window = new window::Window;
  189. }
  190. // On Mac, use a hacked-in GLFW addition to get the launched path.
  191. #if defined ARCH_MAC
  192. // For some reason, launching from the command line sets glfwGetOpenedFilenames(), so make sure we're running the app bundle.
  193. if (asset::bundlePath != "") {
  194. const char* const* openedFilenames = glfwGetOpenedFilenames();
  195. if (openedFilenames && openedFilenames[0]) {
  196. patchPath = openedFilenames[0];
  197. }
  198. }
  199. #endif
  200. // Initialize patch
  201. 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?")) {
  202. // Do nothing, which leaves a blank patch
  203. }
  204. else {
  205. APP->patch->launch(patchPath);
  206. }
  207. APP->engine->startFallbackThread();
  208. // Run context
  209. if (settings::headless) {
  210. printf("Press enter to exit.\n");
  211. getchar();
  212. }
  213. else if (screenshot) {
  214. INFO("Taking screenshots of all modules at %gx zoom", screenshotZoom);
  215. APP->window->screenshotModules(asset::user("screenshots"), screenshotZoom);
  216. }
  217. else {
  218. INFO("Running window");
  219. APP->window->run();
  220. INFO("Stopped window");
  221. // INFO("Destroying window");
  222. // delete APP->window;
  223. // APP->window = NULL;
  224. // INFO("Re-creating window");
  225. // APP->window = new window::Window;
  226. // APP->window->run();
  227. }
  228. // Destroy context
  229. INFO("Deleting context");
  230. delete APP;
  231. contextSet(NULL);
  232. if (!settings::headless) {
  233. settings::save();
  234. }
  235. // Destroy environment
  236. if (!settings::headless) {
  237. INFO("Destroying window");
  238. window::destroy();
  239. INFO("Destroying UI");
  240. ui::destroy();
  241. }
  242. INFO("Destroying library");
  243. library::destroy();
  244. INFO("Destroying MIDI");
  245. midi::destroy();
  246. INFO("Destroying audio");
  247. audio::destroy();
  248. INFO("Destroying plugins");
  249. plugin::destroy();
  250. INFO("Destroying network");
  251. network::destroy();
  252. settings::destroy();
  253. INFO("Destroying logger");
  254. logger::destroy();
  255. return 0;
  256. }
  257. #ifdef UNICODE
  258. /** UTF-16 to UTF-8 wrapper for Windows with unicode */
  259. int wmain(int argc, wchar_t* argvU16[]) {
  260. // Initialize char* array with string-owned buffers
  261. std::string argvStr[argc];
  262. const char* argvU8[argc + 1];
  263. for (int i = 0; i < argc; i++) {
  264. argvStr[i] = string::UTF16toUTF8(argvU16[i]);
  265. argvU8[i] = argvStr[i].c_str();
  266. }
  267. argvU8[argc] = NULL;
  268. return main(argc, (char**) argvU8);
  269. }
  270. #endif