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.

289 lines
7.2KB

  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. #include <string.h> // for sys_siglist
  31. #if defined ARCH_WIN
  32. #include <windows.h> // for CreateMutex
  33. #endif
  34. #if defined ARCH_MAC
  35. #define GLFW_EXPOSE_NATIVE_COCOA
  36. #include <GLFW/glfw3native.h> // for glfwGetOpenedFilenames()
  37. #endif
  38. using namespace rack;
  39. static void fatalSignalHandler(int sig) {
  40. // Ignore this signal to avoid recursion.
  41. signal(sig, NULL);
  42. // Ignore abort() since we call it below.
  43. signal(SIGABRT, NULL);
  44. #if defined ARCH_LIN
  45. const char* sigNameC = strsignal(sig);
  46. #elif defined ARCH_MAC
  47. const char* sigNameC = sys_siglist[sig];
  48. #else
  49. const char* sigNameC = "";
  50. #endif
  51. std::string sigName = "SIG" + string::uppercase(sigNameC);
  52. std::string stackTrace = system::getStackTrace();
  53. FATAL("Fatal signal %d %s. Stack trace:\n%s", sig, sigName.c_str(), stackTrace.c_str());
  54. abort();
  55. }
  56. int main(int argc, char* argv[]) {
  57. #if defined ARCH_WIN
  58. // Windows global mutex to prevent multiple instances
  59. // Handle will be closed by Windows when the process ends
  60. HANDLE instanceMutex = CreateMutexW(NULL, true, string::UTF8toUTF16(APP_NAME).c_str());
  61. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  62. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Rack is already running. Multiple Rack instances are not supported.");
  63. exit(1);
  64. }
  65. (void) instanceMutex;
  66. // Don't display "Assertion failed!" dialog message.
  67. _set_error_mode(_OUT_TO_STDERR);
  68. #endif
  69. std::string patchPath;
  70. bool screenshot = false;
  71. float screenshotZoom = 1.f;
  72. // Parse command line arguments
  73. int c;
  74. opterr = 0;
  75. while ((c = getopt(argc, argv, "dht:s:u:p:")) != -1) {
  76. switch (c) {
  77. case 'd': {
  78. settings::devMode = true;
  79. } break;
  80. case 'h': {
  81. settings::headless = true;
  82. } break;
  83. case 't': {
  84. screenshot = true;
  85. std::sscanf(optarg, "%f", &screenshotZoom);
  86. } break;
  87. case 's': {
  88. asset::systemDir = optarg;
  89. } break;
  90. case 'u': {
  91. asset::userDir = optarg;
  92. } break;
  93. // Mac "app translocation" passes a nonsense -psn_... flag, so -p is reserved.
  94. case 'p': break;
  95. default: break;
  96. }
  97. }
  98. if (optind < argc) {
  99. patchPath = argv[optind];
  100. }
  101. // Initialize environment
  102. system::init();
  103. asset::init();
  104. if (!settings::devMode) {
  105. logger::logPath = asset::user("log.txt");
  106. }
  107. logger::init();
  108. random::init();
  109. // Test code
  110. // exit(0);
  111. // We can now install a signal handler and log the output
  112. if (!settings::devMode) {
  113. signal(SIGABRT, fatalSignalHandler);
  114. signal(SIGFPE, fatalSignalHandler);
  115. signal(SIGILL, fatalSignalHandler);
  116. signal(SIGSEGV, fatalSignalHandler);
  117. signal(SIGTERM, fatalSignalHandler);
  118. }
  119. // Log environment
  120. INFO("%s %s v%s", APP_NAME.c_str(), APP_EDITION_NAME.c_str(), APP_VERSION.c_str());
  121. INFO("%s", system::getOperatingSystemInfo().c_str());
  122. std::string argsList;
  123. for (int i = 0; i < argc; i++) {
  124. argsList += argv[i];
  125. argsList += " ";
  126. }
  127. INFO("Args: %s", argsList.c_str());
  128. if (settings::devMode)
  129. INFO("Development mode");
  130. INFO("System directory: %s", asset::systemDir.c_str());
  131. INFO("User directory: %s", asset::userDir.c_str());
  132. #if defined ARCH_MAC
  133. INFO("Bundle path: %s", asset::bundlePath.c_str());
  134. #endif
  135. INFO("System time: %s", string::formatTimeISO(system::getUnixTime()).c_str());
  136. // Load settings
  137. settings::init();
  138. try {
  139. settings::load();
  140. }
  141. catch (Exception& e) {
  142. std::string msg = e.what();
  143. msg += "\n\nReset settings to default?";
  144. if (!osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK_CANCEL, msg.c_str())) {
  145. exit(1);
  146. }
  147. }
  148. // Check existence of the system res/ directory
  149. std::string resDir = asset::system("res");
  150. if (!system::isDirectory(resDir)) {
  151. std::string message = string::f("Rack's resource directory \"%s\" does not exist. Make sure Rack is correctly installed and launched.", resDir.c_str());
  152. osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, message.c_str());
  153. exit(1);
  154. }
  155. INFO("Initializing environment");
  156. network::init();
  157. audio::init();
  158. rtaudioInit();
  159. midi::init();
  160. rtmidiInit();
  161. keyboard::init();
  162. gamepad::init();
  163. plugin::init();
  164. app::browserInit();
  165. library::init();
  166. discord::init();
  167. if (!settings::headless) {
  168. ui::init();
  169. window::init();
  170. }
  171. // Initialize context
  172. INFO("Initializing context");
  173. contextSet(new Context);
  174. APP->engine = new engine::Engine;
  175. APP->history = new history::State;
  176. APP->event = new widget::EventState;
  177. APP->scene = new app::Scene;
  178. APP->event->rootWidget = APP->scene;
  179. APP->patch = new patch::Manager;
  180. if (!settings::headless) {
  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("Destroying context");
  223. delete APP;
  224. contextSet(NULL);
  225. if (!settings::headless) {
  226. settings::save();
  227. }
  228. // Destroy environment
  229. INFO("Destroying environment");
  230. if (!settings::headless) {
  231. window::destroy();
  232. ui::destroy();
  233. }
  234. discord::destroy();
  235. library::destroy();
  236. midi::destroy();
  237. audio::destroy();
  238. plugin::destroy();
  239. network::destroy();
  240. INFO("Destroying logger");
  241. logger::destroy();
  242. return 0;
  243. }
  244. #ifdef UNICODE
  245. /** UTF-16 to UTF-8 wrapper for Windows with unicode */
  246. int wmain(int argc, wchar_t* argvU16[]) {
  247. // Initialize char* array with string-owned buffers
  248. std::string argvStr[argc];
  249. const char* argvU8[argc + 1];
  250. for (int i = 0; i < argc; i++) {
  251. argvStr[i] = string::UTF16toUTF8(argvU16[i]);
  252. argvU8[i] = argvStr[i].c_str();
  253. }
  254. argvU8[argc] = NULL;
  255. return main(argc, (char**) argvU8);
  256. }
  257. #endif