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.

240 lines
5.9KB

  1. #include "global_pre.hpp"
  2. #include "util/common.hpp"
  3. #include "engine.hpp"
  4. #include "window.hpp"
  5. #include "app.hpp"
  6. #include "plugin.hpp"
  7. #include "settings.hpp"
  8. #include "asset.hpp"
  9. #include "bridge.hpp"
  10. #include "midi.hpp"
  11. #include "rtmidi.hpp"
  12. #include "vstmidi.hpp"
  13. #include "keyboard.hpp"
  14. #include "gamepad.hpp"
  15. #include "util/color.hpp"
  16. #include "osdialog.h"
  17. #ifdef YAC_POSIX
  18. #include <unistd.h>
  19. #endif
  20. #include "global.hpp"
  21. #include "global_ui.hpp"
  22. using namespace rack;
  23. YAC_TLS rack::Global *rack::global;
  24. YAC_TLS rack::GlobalUI *rack::global_ui;
  25. #ifdef USE_VST2
  26. int vst2_init(int argc, char* argv[]) {
  27. bool devMode = false;
  28. std::string patchFile;
  29. #if 0
  30. // Parse command line arguments
  31. int c;
  32. opterr = 0;
  33. while ((c = getopt(argc, argv, "d")) != -1) {
  34. switch (c) {
  35. case 'd': {
  36. devMode = true;
  37. } break;
  38. default: break;
  39. }
  40. }
  41. if (optind < argc) {
  42. patchFile = argv[optind];
  43. }
  44. #endif
  45. // Initialize environment
  46. randomInit();
  47. assetInit(devMode);
  48. loggerInit(devMode);
  49. // Log environment
  50. info("%s %s", global_ui->app.gApplicationName.c_str(), global_ui->app.gApplicationVersion.c_str());
  51. if (devMode)
  52. info("Development mode");
  53. info("Global directory: %s", assetGlobal("").c_str());
  54. info("Local directory: %s", assetLocal("").c_str());
  55. // Initialize app
  56. pluginInit(devMode);
  57. engineInit();
  58. #ifndef USE_VST2
  59. rtmidiInit();
  60. bridgeInit();
  61. #endif // USE_VST2
  62. vstmidiInit();
  63. #ifndef USE_VST2
  64. keyboardInit();
  65. gamepadInit();
  66. #endif // USE_VST2
  67. windowInit();
  68. appInit(devMode);
  69. settingsLoad(assetLocal("settings.json"), false/*bWindowSizeOnly*/);
  70. #if 0
  71. if (patchFile.empty()) {
  72. std::string oldLastPath = global_ui->app.gRackWidget->lastPath;
  73. // To prevent launch crashes, if Rack crashes between now and 15 seconds from now, the "skipAutosaveOnLaunch" property will remain in settings.json, so that in the next launch, the broken autosave will not be loaded.
  74. bool oldSkipAutosaveOnLaunch = global->settings.gSkipAutosaveOnLaunch;
  75. global->settings.gSkipAutosaveOnLaunch = true;
  76. settingsSave(assetLocal("settings.json"));
  77. global->settings.gSkipAutosaveOnLaunch = false;
  78. if (oldSkipAutosaveOnLaunch && osdialog_message(OSDIALOG_INFO, OSDIALOG_YES_NO, "Rack has recovered from a crash, possibly caused by a faulty module in your patch. Would you like to clear your patch and start over?")) {
  79. // Do nothing. Empty patch is already loaded.
  80. }
  81. else {
  82. // Load autosave
  83. global_ui->app.gRackWidget->loadPatch(assetLocal("autosave.vcv"));
  84. }
  85. global_ui->app.gRackWidget->lastPath = oldLastPath;
  86. }
  87. else {
  88. // Load patch
  89. global_ui->app.gRackWidget->loadPatch(patchFile);
  90. }
  91. #endif
  92. // // engineStart(); // starts bg thread for audio rendering
  93. return 0;
  94. }
  95. void vst2_exit(void) {
  96. // Destroy namespaces
  97. // // engineStop();
  98. printf("xxx vst2_exit 1\n");
  99. // global_ui->app.gRackWidget->savePatch(assetLocal("autosave.vcv"));
  100. // settingsSave(assetLocal("settings.json"));
  101. printf("xxx vst2_exit 2\n");
  102. #if 1
  103. lglw_glcontext_push(global_ui->window.lglw);
  104. appDestroy();
  105. lglw_glcontext_pop(global_ui->window.lglw);
  106. #endif
  107. printf("xxx vst2_exit 3\n");
  108. windowDestroy();
  109. #if 0
  110. #ifndef USE_VST2
  111. bridgeDestroy();
  112. #endif // USE_VST2
  113. printf("xxx vst2_exit 4\n");
  114. engineDestroy();
  115. printf("xxx vst2_exit 5\n");
  116. #endif
  117. printf("xxx vst2_exit destroy midi\n");
  118. midiDestroy();
  119. printf("xxx vst2_exit destroy midi done\n");
  120. #if 1
  121. printf("xxx vst2_exit 6\n");
  122. pluginDestroy();
  123. printf("xxx vst2_exit 7\n");
  124. loggerDestroy();
  125. #endif
  126. printf("xxx vst2_exit 8 (leave)\n");
  127. }
  128. #else
  129. static rack::Global loc_global;
  130. static rack::GlobalUI loc_global_ui;
  131. int main(int argc, char* argv[]) {
  132. bool devMode = false;
  133. std::string patchFile;
  134. loc_global.init();
  135. loc_global_ui.init();
  136. rack::global = &loc_global;
  137. rack::global_ui = &loc_global_ui;
  138. // Parse command line arguments
  139. int c;
  140. opterr = 0;
  141. while ((c = getopt(argc, argv, "d")) != -1) {
  142. switch (c) {
  143. case 'd': {
  144. devMode = true;
  145. } break;
  146. default: break;
  147. }
  148. }
  149. if (optind < argc) {
  150. patchFile = argv[optind];
  151. }
  152. // Initialize environment
  153. randomInit();
  154. assetInit(devMode);
  155. loggerInit(devMode);
  156. // Log environment
  157. info("%s %s", global_ui->app.gApplicationName.c_str(), global_ui->app.gApplicationVersion.c_str());
  158. if (devMode)
  159. info("Development mode");
  160. info("Global directory: %s", assetGlobal("").c_str());
  161. info("Local directory: %s", assetLocal("").c_str());
  162. // Initialize app
  163. pluginInit(devMode);
  164. engineInit();
  165. rtmidiInit();
  166. bridgeInit();
  167. keyboardInit();
  168. gamepadInit();
  169. windowInit();
  170. appInit(devMode);
  171. settingsLoad(assetLocal("settings.json"));
  172. if (patchFile.empty()) {
  173. std::string oldLastPath = global_ui->app.gRackWidget->lastPath;
  174. // To prevent launch crashes, if Rack crashes between now and 15 seconds from now, the "skipAutosaveOnLaunch" property will remain in settings.json, so that in the next launch, the broken autosave will not be loaded.
  175. bool oldSkipAutosaveOnLaunch = global->settings.gSkipAutosaveOnLaunch;
  176. global->settings.gSkipAutosaveOnLaunch = true;
  177. settingsSave(assetLocal("settings.json"));
  178. global->settings.gSkipAutosaveOnLaunch = false;
  179. if (oldSkipAutosaveOnLaunch && osdialog_message(OSDIALOG_INFO, OSDIALOG_YES_NO, "Rack has recovered from a crash, possibly caused by a faulty module in your patch. Would you like to clear your patch and start over?")) {
  180. // Do nothing. Empty patch is already loaded.
  181. }
  182. else {
  183. // Load autosave
  184. global_ui->app.gRackWidget->loadPatch(assetLocal("autosave.vcv"));
  185. }
  186. global_ui->app.gRackWidget->lastPath = oldLastPath;
  187. }
  188. else {
  189. // Load patch
  190. global_ui->app.gRackWidget->loadPatch(patchFile);
  191. }
  192. engineStart();
  193. windowRun();
  194. engineStop();
  195. // Destroy namespaces
  196. global_ui->app.gRackWidget->savePatch(assetLocal("autosave.vcv"));
  197. settingsSave(assetLocal("settings.json"));
  198. appDestroy();
  199. windowDestroy();
  200. bridgeDestroy();
  201. engineDestroy();
  202. midiDestroy();
  203. pluginDestroy();
  204. loggerDestroy();
  205. return 0;
  206. }
  207. #endif // USE_VST2