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