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.

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