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.

281 lines
7.0KB

  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. // Ignore abort() since we call it below.
  42. signal(SIGABRT, NULL);
  43. std::string sigName = "SIG" + string::uppercase(sys_signame[sig]);
  44. std::string stackTrace = system::getStackTrace();
  45. FATAL("Fatal signal %d %s. Stack trace:\n%s", sig, sigName.c_str(), stackTrace.c_str());
  46. abort();
  47. }
  48. int main(int argc, char* argv[]) {
  49. #if defined ARCH_WIN
  50. // Windows global mutex to prevent multiple instances
  51. // Handle will be closed by Windows when the process ends
  52. HANDLE instanceMutex = CreateMutexW(NULL, true, string::UTF8toUTF16(APP_NAME).c_str());
  53. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  54. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Rack is already running. Multiple Rack instances are not supported.");
  55. exit(1);
  56. }
  57. (void) instanceMutex;
  58. // Don't display "Assertion failed!" dialog message.
  59. _set_error_mode(_OUT_TO_STDERR);
  60. #endif
  61. std::string patchPath;
  62. bool screenshot = false;
  63. float screenshotZoom = 1.f;
  64. // Parse command line arguments
  65. int c;
  66. opterr = 0;
  67. while ((c = getopt(argc, argv, "dht:s:u:p:")) != -1) {
  68. switch (c) {
  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 environment");
  148. network::init();
  149. audio::init();
  150. rtaudioInit();
  151. midi::init();
  152. rtmidiInit();
  153. keyboard::init();
  154. gamepad::init();
  155. plugin::init();
  156. app::browserInit();
  157. library::init();
  158. discord::init();
  159. if (!settings::headless) {
  160. ui::init();
  161. window::init();
  162. }
  163. // Initialize context
  164. INFO("Initializing context");
  165. contextSet(new Context);
  166. APP->engine = new engine::Engine;
  167. APP->history = new history::State;
  168. APP->event = new widget::EventState;
  169. APP->scene = new app::Scene;
  170. APP->event->rootWidget = APP->scene;
  171. APP->patch = new patch::Manager;
  172. if (!settings::headless) {
  173. APP->window = new window::Window;
  174. }
  175. // On Mac, use a hacked-in GLFW addition to get the launched path.
  176. #if defined ARCH_MAC
  177. // For some reason, launching from the command line sets glfwGetOpenedFilenames(), so make sure we're running the app bundle.
  178. if (asset::bundlePath != "") {
  179. // const char* const* openedFilenames = glfwGetOpenedFilenames();
  180. // if (openedFilenames && openedFilenames[0]) {
  181. // patchPath = openedFilenames[0];
  182. // }
  183. }
  184. #endif
  185. // Initialize patch
  186. 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?")) {
  187. // Do nothing, which leaves a blank patch
  188. }
  189. else {
  190. APP->patch->launch(patchPath);
  191. }
  192. APP->engine->startFallbackThread();
  193. // Run context
  194. if (settings::headless) {
  195. printf("Press enter to exit.\n");
  196. getchar();
  197. }
  198. else if (screenshot) {
  199. INFO("Taking screenshots of all modules at %gx zoom", screenshotZoom);
  200. APP->window->screenshotModules(asset::user("screenshots"), screenshotZoom);
  201. }
  202. else {
  203. INFO("Running window");
  204. APP->window->run();
  205. INFO("Stopped window");
  206. // INFO("Destroying window");
  207. // delete APP->window;
  208. // APP->window = NULL;
  209. // INFO("Re-creating window");
  210. // APP->window = new window::Window;
  211. // APP->window->run();
  212. }
  213. // Destroy context
  214. INFO("Destroying context");
  215. delete APP;
  216. contextSet(NULL);
  217. if (!settings::headless) {
  218. settings::save();
  219. }
  220. // Destroy environment
  221. INFO("Destroying environment");
  222. if (!settings::headless) {
  223. window::destroy();
  224. ui::destroy();
  225. }
  226. discord::destroy();
  227. library::destroy();
  228. midi::destroy();
  229. audio::destroy();
  230. plugin::destroy();
  231. network::destroy();
  232. INFO("Destroying logger");
  233. logger::destroy();
  234. return 0;
  235. }
  236. #ifdef UNICODE
  237. /** UTF-16 to UTF-8 wrapper for Windows with unicode */
  238. int wmain(int argc, wchar_t* argvU16[]) {
  239. // Initialize char* array with string-owned buffers
  240. std::string argvStr[argc];
  241. const char* argvU8[argc + 1];
  242. for (int i = 0; i < argc; i++) {
  243. argvStr[i] = string::UTF16toUTF8(argvU16[i]);
  244. argvU8[i] = argvStr[i].c_str();
  245. }
  246. argvU8[argc] = NULL;
  247. return main(argc, (char**) argvU8);
  248. }
  249. #endif