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.

257 lines
6.4KB

  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[], bool _bFX) {
  27. #ifdef YAC_LINUX
  28. bool devMode = true; // true=log to stderr, false=log to file
  29. #else
  30. bool devMode = false;
  31. #endif // YAC_LINUX
  32. std::string patchFile;
  33. #if 0
  34. // Parse command line arguments
  35. int c;
  36. opterr = 0;
  37. while ((c = getopt(argc, argv, "d")) != -1) {
  38. switch (c) {
  39. case 'd': {
  40. devMode = true;
  41. } break;
  42. default: break;
  43. }
  44. }
  45. if (optind < argc) {
  46. patchFile = argv[optind];
  47. }
  48. #endif
  49. printf("xxx vst2_init: 1\n");
  50. // Initialize environment
  51. randomInit();
  52. printf("xxx vst2_init: 2\n");
  53. assetInit(devMode);
  54. printf("xxx vst2_init: 3\n");
  55. loggerInit(devMode);
  56. printf("xxx vst2_init: 4 global_ui=%p\n", global_ui);
  57. // Log environment
  58. info("%s %s", global_ui->app.gApplicationName.c_str(), global_ui->app.gApplicationVersion.c_str());
  59. if (devMode)
  60. info("Development mode");
  61. info("Global directory: %s", assetGlobal("").c_str());
  62. info("Local directory: %s", assetLocal("").c_str());
  63. printf("xxx vst2_init: 5\n");
  64. // Initialize app
  65. pluginInit(devMode, _bFX);
  66. printf("xxx vst2_init: 6\n");
  67. engineInit();
  68. printf("xxx vst2_init: 7\n");
  69. #ifndef USE_VST2
  70. rtmidiInit();
  71. bridgeInit();
  72. #endif // USE_VST2
  73. vstmidiInit();
  74. printf("xxx vst2_init: 8\n");
  75. #ifndef USE_VST2
  76. keyboardInit();
  77. gamepadInit();
  78. #endif // USE_VST2
  79. windowInit();
  80. printf("xxx vst2_init: 9\n");
  81. appInit(devMode);
  82. printf("xxx vst2_init: 10\n");
  83. settingsLoad(assetLocal("settings.json"), false/*bWindowSizeOnly*/);
  84. printf("xxx vst2_init: 11\n");
  85. #if 0
  86. if (patchFile.empty()) {
  87. std::string oldLastPath = global_ui->app.gRackWidget->lastPath;
  88. // 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.
  89. bool oldSkipAutosaveOnLaunch = global->settings.gSkipAutosaveOnLaunch;
  90. global->settings.gSkipAutosaveOnLaunch = true;
  91. settingsSave(assetLocal("settings.json"));
  92. global->settings.gSkipAutosaveOnLaunch = false;
  93. 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?")) {
  94. // Do nothing. Empty patch is already loaded.
  95. }
  96. else {
  97. // Load autosave
  98. global_ui->app.gRackWidget->loadPatch(assetLocal("autosave.vcv"));
  99. }
  100. global_ui->app.gRackWidget->lastPath = oldLastPath;
  101. }
  102. else {
  103. // Load patch
  104. global_ui->app.gRackWidget->loadPatch(patchFile);
  105. }
  106. #endif
  107. // // engineStart(); // starts bg thread for audio rendering
  108. printf("xxx vst2_init: LEAVE\n");
  109. return 0;
  110. }
  111. void vst2_exit(void) {
  112. // Destroy namespaces
  113. // // engineStop();
  114. printf("xxx vst2_exit 1\n");
  115. // global_ui->app.gRackWidget->savePatch(assetLocal("autosave.vcv"));
  116. // settingsSave(assetLocal("settings.json"));
  117. printf("xxx vst2_exit 2\n");
  118. #if 1
  119. lglw_glcontext_push(global_ui->window.lglw);
  120. appDestroy();
  121. lglw_glcontext_pop(global_ui->window.lglw);
  122. #endif
  123. printf("xxx vst2_exit 3\n");
  124. windowDestroy();
  125. #if 0
  126. #ifndef USE_VST2
  127. bridgeDestroy();
  128. #endif // USE_VST2
  129. printf("xxx vst2_exit 4\n");
  130. engineDestroy();
  131. printf("xxx vst2_exit 5\n");
  132. #endif
  133. printf("xxx vst2_exit destroy midi\n");
  134. midiDestroy();
  135. printf("xxx vst2_exit destroy midi done\n");
  136. #if 1
  137. printf("xxx vst2_exit 6\n");
  138. pluginDestroy();
  139. printf("xxx vst2_exit 7\n");
  140. loggerDestroy();
  141. #endif
  142. printf("xxx vst2_exit 8 (leave)\n");
  143. }
  144. #else
  145. static rack::Global loc_global;
  146. static rack::GlobalUI loc_global_ui;
  147. int main(int argc, char* argv[]) {
  148. bool devMode = false;
  149. std::string patchFile;
  150. loc_global.init();
  151. loc_global_ui.init();
  152. rack::global = &loc_global;
  153. rack::global_ui = &loc_global_ui;
  154. // Parse command line arguments
  155. int c;
  156. opterr = 0;
  157. while ((c = getopt(argc, argv, "d")) != -1) {
  158. switch (c) {
  159. case 'd': {
  160. devMode = true;
  161. } break;
  162. default: break;
  163. }
  164. }
  165. if (optind < argc) {
  166. patchFile = argv[optind];
  167. }
  168. // Initialize environment
  169. randomInit();
  170. assetInit(devMode);
  171. loggerInit(devMode);
  172. // Log environment
  173. info("%s %s", global_ui->app.gApplicationName.c_str(), global_ui->app.gApplicationVersion.c_str());
  174. if (devMode)
  175. info("Development mode");
  176. info("Global directory: %s", assetGlobal("").c_str());
  177. info("Local directory: %s", assetLocal("").c_str());
  178. // Initialize app
  179. pluginInit(devMode);
  180. engineInit();
  181. rtmidiInit();
  182. bridgeInit();
  183. keyboardInit();
  184. gamepadInit();
  185. windowInit();
  186. appInit(devMode);
  187. settingsLoad(assetLocal("settings.json"));
  188. if (patchFile.empty()) {
  189. std::string oldLastPath = global_ui->app.gRackWidget->lastPath;
  190. // 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.
  191. bool oldSkipAutosaveOnLaunch = global->settings.gSkipAutosaveOnLaunch;
  192. global->settings.gSkipAutosaveOnLaunch = true;
  193. settingsSave(assetLocal("settings.json"));
  194. global->settings.gSkipAutosaveOnLaunch = false;
  195. 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?")) {
  196. // Do nothing. Empty patch is already loaded.
  197. }
  198. else {
  199. // Load autosave
  200. global_ui->app.gRackWidget->loadPatch(assetLocal("autosave.vcv"));
  201. }
  202. global_ui->app.gRackWidget->lastPath = oldLastPath;
  203. }
  204. else {
  205. // Load patch
  206. global_ui->app.gRackWidget->loadPatch(patchFile);
  207. }
  208. engineStart();
  209. windowRun();
  210. engineStop();
  211. // Destroy namespaces
  212. global_ui->app.gRackWidget->savePatch(assetLocal("autosave.vcv"));
  213. settingsSave(assetLocal("settings.json"));
  214. appDestroy();
  215. windowDestroy();
  216. bridgeDestroy();
  217. engineDestroy();
  218. midiDestroy();
  219. pluginDestroy();
  220. loggerDestroy();
  221. return 0;
  222. }
  223. #endif // USE_VST2