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.

1816 lines
60KB

  1. /*
  2. * DISTRHO Ildaeil Plugin
  3. * Copyright (C) 2021-2025 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the LICENSE file.
  16. */
  17. #include "IldaeilBasePlugin.hpp"
  18. #include "DistrhoPluginUtils.hpp"
  19. #include "DistrhoUI.hpp"
  20. #if ILDAEIL_STANDALONE
  21. #include "DistrhoStandaloneUtils.hpp"
  22. #endif
  23. #include "CarlaBackendUtils.hpp"
  24. #include "PluginHostWindow.hpp"
  25. #include "extra/Runner.hpp"
  26. // IDE helper
  27. #include "DearImGui.hpp"
  28. #include "water/files/File.h"
  29. #include "water/files/FileInputStream.h"
  30. #include "water/files/FileOutputStream.h"
  31. #include "water/memory/MemoryBlock.h"
  32. #include <string>
  33. #include <vector>
  34. // strcasestr
  35. #ifdef DISTRHO_OS_WINDOWS
  36. # include <shlwapi.h>
  37. namespace ildaeil {
  38. inline const char* strcasestr(const char* const haystack, const char* const needle)
  39. {
  40. return StrStrIA(haystack, needle);
  41. }
  42. // using strcasestr = StrStrIA;
  43. }
  44. #else
  45. namespace ildaeil {
  46. using ::strcasestr;
  47. }
  48. #endif
  49. // #define WASM_TESTING
  50. START_NAMESPACE_DISTRHO
  51. using namespace CARLA_BACKEND_NAMESPACE;
  52. // --------------------------------------------------------------------------------------------------------------------
  53. class IldaeilUI : public UI,
  54. public Runner,
  55. public PluginHostWindow::Callbacks
  56. {
  57. static constexpr const uint kGenericWidth = 380;
  58. static constexpr const uint kGenericHeight = 400;
  59. static constexpr const uint kButtonHeight = 20;
  60. struct PluginInfoCache {
  61. BinaryType btype;
  62. uint64_t uniqueId;
  63. std::string filename;
  64. std::string name;
  65. std::string label;
  66. };
  67. struct PluginGenericUI {
  68. char* title = nullptr;
  69. uint parameterCount = 0;
  70. struct Parameter {
  71. char* name = nullptr;
  72. char* printformat = nullptr;
  73. uint32_t rindex = 0;
  74. bool boolean = false;
  75. bool bvalue = false;
  76. bool log = false;
  77. bool readonly = false;
  78. float min = 0.f;
  79. float max = 1.f;
  80. ~Parameter()
  81. {
  82. std::free(name);
  83. std::free(printformat);
  84. }
  85. }* parameters = nullptr;
  86. float* values = nullptr;
  87. uint presetCount = 0;
  88. struct Preset {
  89. uint32_t index = 0;
  90. char* name = nullptr;
  91. ~Preset()
  92. {
  93. std::free(name);
  94. }
  95. }* presets = nullptr;
  96. int currentPreset = -1;
  97. const char** presetStrings = nullptr;
  98. ~PluginGenericUI()
  99. {
  100. std::free(title);
  101. delete[] parameters;
  102. delete[] values;
  103. delete[] presets;
  104. delete[] presetStrings;
  105. }
  106. };
  107. enum {
  108. kDrawingLoading,
  109. kDrawingPluginError,
  110. kDrawingPluginList,
  111. kDrawingPluginEmbedUI,
  112. kDrawingPluginGenericUI,
  113. kDrawingErrorInit,
  114. kDrawingErrorDraw
  115. } fDrawingState = kDrawingLoading;
  116. enum {
  117. kIdleInit,
  118. kIdleInitPluginAlreadyLoaded,
  119. kIdleLoadSelectedPlugin,
  120. kIdlePluginLoadedFromDSP,
  121. kIdleResetPlugin,
  122. kIdleOpenFileUI,
  123. kIdleShowCustomUI,
  124. kIdleHideEmbedAndShowGenericUI,
  125. kIdleHidePluginUI,
  126. kIdleGiveIdleToUI,
  127. kIdleChangePluginType,
  128. kIdleNothing
  129. } fIdleState = kIdleInit;
  130. IldaeilBasePlugin* const fPlugin = static_cast<IldaeilBasePlugin*>(getPluginInstancePointer());
  131. void* const fNativeWindowHandle = reinterpret_cast<void*>(getWindow().getNativeWindowHandle());
  132. PluginHostWindow fPluginHostWindow { fNativeWindowHandle, this };
  133. BinaryType fBinaryType = BINARY_NATIVE;
  134. PluginType fPluginType = PLUGIN_LV2;
  135. PluginType fNextPluginType = fPluginType;
  136. uint fPluginId = 0;
  137. int fPluginSelected = -1;
  138. bool fPluginHasCustomUI = false;
  139. bool fPluginHasEmbedUI = false;
  140. bool fPluginHasFileOpen = false;
  141. bool fPluginHasOutputParameters = false;
  142. bool fPluginIsBridge = false;
  143. bool fPluginIsIdling = false;
  144. bool fPluginRunning = false;
  145. bool fPluginWillRunInBridgeMode = false;
  146. Mutex fPluginsMutex;
  147. PluginInfoCache fCurrentPluginInfo{};
  148. std::vector<PluginInfoCache> fPlugins;
  149. ScopedPointer<PluginGenericUI> fPluginGenericUI;
  150. bool fPluginSearchActive = false;
  151. bool fPluginSearchFirstShow = false;
  152. char fPluginSearchString[0xff] = {};
  153. String fPopupError, fPluginFilename, fDiscoveryTool;
  154. Size<uint> fCurrentConstraintSize, fLastSize, fNextSize;
  155. bool fIgnoreNextHostWindowResize = false;
  156. bool fShowingHostWindow = false;
  157. bool fUpdateGeometryConstraints = false;
  158. struct RunnerData {
  159. bool needsReinit = true;
  160. CarlaPluginDiscoveryHandle handle = nullptr;
  161. void init()
  162. {
  163. needsReinit = true;
  164. if (handle != nullptr)
  165. {
  166. carla_plugin_discovery_stop(handle);
  167. handle = nullptr;
  168. }
  169. }
  170. } fRunnerData;
  171. public:
  172. IldaeilUI()
  173. : UI(kInitialWidth, kInitialHeight),
  174. Runner("IldaeilScanner")
  175. {
  176. const double scaleFactor = getScaleFactor();
  177. if (fPlugin == nullptr || fPlugin->fCarlaHostHandle == nullptr)
  178. {
  179. fDrawingState = kDrawingErrorInit;
  180. fIdleState = kIdleNothing;
  181. fPopupError = "Ildaeil backend failed to init properly, cannot continue.";
  182. setGeometryConstraints(kInitialWidth * scaleFactor * 0.5, kInitialHeight * scaleFactor * 0.5);
  183. setSize(kInitialWidth * scaleFactor * 0.5, kInitialHeight * scaleFactor * 0.5);
  184. return;
  185. }
  186. std::strcpy(fPluginSearchString, "Search...");
  187. ImGuiStyle& style(ImGui::GetStyle());
  188. style.FrameRounding = 4 * scaleFactor;
  189. const double paddingY = style.WindowPadding.y * 2;
  190. if (d_isNotEqual(scaleFactor, 1.0))
  191. {
  192. fPluginHostWindow.setOffset(0, kButtonHeight * scaleFactor + paddingY);
  193. setGeometryConstraints(kInitialWidth * scaleFactor, kInitialWidth * scaleFactor);
  194. setSize(kInitialWidth * scaleFactor, kInitialHeight * scaleFactor);
  195. }
  196. else
  197. {
  198. fPluginHostWindow.setOffset(0, kButtonHeight + paddingY);
  199. setGeometryConstraints(kInitialWidth, kInitialWidth);
  200. }
  201. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  202. char winIdStr[24];
  203. std::snprintf(winIdStr, sizeof(winIdStr), "%lx", (ulong)getWindow().getNativeWindowHandle());
  204. carla_set_engine_option(handle, ENGINE_OPTION_FRONTEND_WIN_ID, 0, winIdStr);
  205. carla_set_engine_option(handle, ENGINE_OPTION_FRONTEND_UI_SCALE, scaleFactor*1000, nullptr);
  206. if (checkIfPluginIsLoaded())
  207. fIdleState = kIdleInitPluginAlreadyLoaded;
  208. fPlugin->fUI = this;
  209. #ifdef WASM_TESTING
  210. if (carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr,
  211. "midifile", 0, 0x0, PLUGIN_OPTIONS_NULL))
  212. {
  213. d_stdout("Special hack for MIDI file playback activated");
  214. carla_set_custom_data(handle, 0, CUSTOM_DATA_TYPE_PATH, "file", "/furelise.mid");
  215. carla_set_parameter_value(handle, 0, 0, 1.0f);
  216. carla_set_parameter_value(handle, 0, 1, 0.0f);
  217. fPluginId = 2;
  218. }
  219. carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr, "miditranspose", 0, 0x0, PLUGIN_OPTIONS_NULL);
  220. carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr, "bypass", 0, 0x0, PLUGIN_OPTIONS_NULL);
  221. carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr, "3bandeq", 0, 0x0, PLUGIN_OPTIONS_NULL);
  222. carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr, "pingpongpan", 0, 0x0, PLUGIN_OPTIONS_NULL);
  223. carla_set_parameter_value(handle, 4, 1, 0.0f);
  224. carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr, "audiogain_s", 0, 0x0, PLUGIN_OPTIONS_NULL);
  225. for (uint i=0; i<5; ++i)
  226. carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr, "bypass", 0, 0x0, PLUGIN_OPTIONS_NULL);
  227. #endif
  228. }
  229. ~IldaeilUI() override
  230. {
  231. if (fPlugin != nullptr && fPlugin->fCarlaHostHandle != nullptr)
  232. {
  233. fPlugin->fUI = nullptr;
  234. if (fPluginRunning)
  235. hidePluginUI(fPlugin->fCarlaHostHandle);
  236. carla_set_engine_option(fPlugin->fCarlaHostHandle, ENGINE_OPTION_FRONTEND_WIN_ID, 0, "0");
  237. }
  238. stopRunner();
  239. fPluginGenericUI = nullptr;
  240. }
  241. bool checkIfPluginIsLoaded()
  242. {
  243. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  244. if (carla_get_current_plugin_count(handle) == 0)
  245. return false;
  246. const uint hints = carla_get_plugin_info(handle, fPluginId)->hints;
  247. updatePluginFlags(hints);
  248. fPluginRunning = true;
  249. return true;
  250. }
  251. void updatePluginFlags(const uint hints) noexcept
  252. {
  253. if (hints & PLUGIN_HAS_CUSTOM_UI_USING_FILE_OPEN)
  254. {
  255. fPluginHasCustomUI = false;
  256. fPluginHasEmbedUI = false;
  257. fPluginHasFileOpen = true;
  258. }
  259. else
  260. {
  261. fPluginHasCustomUI = hints & PLUGIN_HAS_CUSTOM_UI;
  262. #ifndef DISTRHO_OS_WASM
  263. fPluginHasEmbedUI = hints & PLUGIN_HAS_CUSTOM_EMBED_UI;
  264. #endif
  265. fPluginHasFileOpen = false;
  266. }
  267. fPluginIsBridge = hints & PLUGIN_IS_BRIDGE;
  268. }
  269. void projectLoadedFromDSP()
  270. {
  271. if (checkIfPluginIsLoaded())
  272. fIdleState = kIdlePluginLoadedFromDSP;
  273. }
  274. void changeParameterFromDSP(const uint32_t index, const float value)
  275. {
  276. if (PluginGenericUI* const ui = fPluginGenericUI)
  277. {
  278. for (uint32_t i=0; i < ui->parameterCount; ++i)
  279. {
  280. if (ui->parameters[i].rindex != index)
  281. continue;
  282. ui->values[i] = value;
  283. if (ui->parameters[i].boolean)
  284. ui->parameters[i].bvalue = value > ui->parameters[i].min;
  285. break;
  286. }
  287. }
  288. repaint();
  289. }
  290. void resizeUI(const uint32_t width, const uint32_t height)
  291. {
  292. if (fDrawingState != kDrawingPluginEmbedUI)
  293. return;
  294. const uint extraHeight = kButtonHeight * getScaleFactor() + ImGui::GetStyle().WindowPadding.y * 2;
  295. fShowingHostWindow = true;
  296. fNextSize = Size<uint>(width, height + extraHeight);
  297. }
  298. void closeUI()
  299. {
  300. if (fIdleState == kIdleGiveIdleToUI)
  301. fIdleState = kIdleNothing;
  302. }
  303. const char* openFileFromDSP(const bool /*isDir*/, const char* const title, const char* const /*filter*/)
  304. {
  305. DISTRHO_SAFE_ASSERT_RETURN(fPluginType == PLUGIN_INTERNAL || fPluginType == PLUGIN_LV2, nullptr);
  306. FileBrowserOptions opts;
  307. opts.title = title;
  308. openFileBrowser(opts);
  309. return nullptr;
  310. }
  311. void showPluginUI(const CarlaHostHandle handle, const bool showIfNotEmbed)
  312. {
  313. #ifndef DISTRHO_OS_WASM
  314. const uint hints = carla_get_plugin_info(handle, fPluginId)->hints;
  315. if (hints & PLUGIN_HAS_CUSTOM_EMBED_UI)
  316. {
  317. fDrawingState = kDrawingPluginEmbedUI;
  318. fIdleState = kIdleGiveIdleToUI;
  319. fPluginHasCustomUI = true;
  320. fPluginHasEmbedUI = true;
  321. fPluginHasFileOpen = false;
  322. fIgnoreNextHostWindowResize = false;
  323. fShowingHostWindow = true;
  324. fPluginHostWindow.restart();
  325. carla_embed_custom_ui(handle, fPluginId, fNativeWindowHandle);
  326. fPluginHostWindow.idle();
  327. }
  328. else
  329. #endif
  330. {
  331. // fPluginHas* flags are updated in the next function
  332. createOrUpdatePluginGenericUI(handle);
  333. if (showIfNotEmbed && fPluginHasCustomUI)
  334. {
  335. fIdleState = kIdleGiveIdleToUI;
  336. carla_show_custom_ui(handle, fPluginId, true);
  337. }
  338. }
  339. repaint();
  340. }
  341. void hidePluginUI(const CarlaHostHandle handle)
  342. {
  343. DISTRHO_SAFE_ASSERT_RETURN(fPluginRunning,);
  344. if (fPluginHostWindow.hide())
  345. carla_show_custom_ui(handle, fPluginId, false);
  346. }
  347. void createOrUpdatePluginGenericUI(const CarlaHostHandle handle, const CarlaPluginInfo* info = nullptr)
  348. {
  349. if (info == nullptr)
  350. info = carla_get_plugin_info(handle, fPluginId);
  351. fDrawingState = kDrawingPluginGenericUI;
  352. updatePluginFlags(info->hints);
  353. if (fPluginGenericUI == nullptr)
  354. createPluginGenericUI(handle, info);
  355. else
  356. updatePluginGenericUI(handle);
  357. #ifndef DISTRHO_OS_WASM
  358. const double scaleFactor = getScaleFactor();
  359. fNextSize = Size<uint>(kGenericWidth * scaleFactor,
  360. (kGenericHeight + ImGui::GetStyle().WindowPadding.y) * scaleFactor);
  361. fLastSize = Size<uint>();
  362. fUpdateGeometryConstraints = true;
  363. #endif
  364. }
  365. void createPluginGenericUI(const CarlaHostHandle handle, const CarlaPluginInfo* const info)
  366. {
  367. PluginGenericUI* const ui = new PluginGenericUI;
  368. String title(info->name);
  369. title += " by ";
  370. title += info->maker;
  371. ui->title = title.getAndReleaseBuffer();
  372. fPluginHasOutputParameters = false;
  373. const uint32_t parameterCount = ui->parameterCount = carla_get_parameter_count(handle, fPluginId);
  374. // make count of valid parameters
  375. for (uint32_t i=0; i < parameterCount; ++i)
  376. {
  377. const ParameterData* const pdata = carla_get_parameter_data(handle, fPluginId, i);
  378. if ((pdata->hints & PARAMETER_IS_ENABLED) == 0x0)
  379. {
  380. --ui->parameterCount;
  381. continue;
  382. }
  383. if (pdata->type == PARAMETER_OUTPUT)
  384. fPluginHasOutputParameters = true;
  385. }
  386. ui->parameters = new PluginGenericUI::Parameter[ui->parameterCount];
  387. ui->values = new float[ui->parameterCount];
  388. // now safely fill in details
  389. for (uint32_t i=0, j=0; i < parameterCount; ++i)
  390. {
  391. const ParameterData* const pdata = carla_get_parameter_data(handle, fPluginId, i);
  392. if ((pdata->hints & PARAMETER_IS_ENABLED) == 0x0)
  393. continue;
  394. const CarlaParameterInfo* const pinfo = carla_get_parameter_info(handle, fPluginId, i);
  395. const ::ParameterRanges* const pranges = carla_get_parameter_ranges(handle, fPluginId, i);
  396. String printformat;
  397. if (pdata->hints & PARAMETER_IS_INTEGER)
  398. printformat = "%.0f ";
  399. else
  400. printformat = "%.3f ";
  401. printformat += pinfo->unit;
  402. PluginGenericUI::Parameter& param(ui->parameters[j]);
  403. param.name = strdup(pinfo->name);
  404. param.printformat = printformat.getAndReleaseBuffer();
  405. param.rindex = i;
  406. param.boolean = pdata->hints & PARAMETER_IS_BOOLEAN;
  407. param.log = pdata->hints & PARAMETER_IS_LOGARITHMIC;
  408. param.readonly = pdata->type != PARAMETER_INPUT || (pdata->hints & PARAMETER_IS_READ_ONLY);
  409. param.min = pranges->min;
  410. param.max = pranges->max;
  411. ui->values[j] = carla_get_current_parameter_value(handle, fPluginId, i);
  412. if (param.boolean)
  413. param.bvalue = ui->values[j] > param.min;
  414. else
  415. param.bvalue = false;
  416. ++j;
  417. }
  418. // handle presets too
  419. const uint32_t presetCount = ui->presetCount = carla_get_program_count(handle, fPluginId);
  420. for (uint32_t i=0; i < presetCount; ++i)
  421. {
  422. const char* const pname = carla_get_program_name(handle, fPluginId, i);
  423. if (pname[0] == '\0')
  424. {
  425. --ui->presetCount;
  426. continue;
  427. }
  428. }
  429. ui->presets = new PluginGenericUI::Preset[ui->presetCount];
  430. ui->presetStrings = new const char*[ui->presetCount];
  431. for (uint32_t i=0, j=0; i < presetCount; ++i)
  432. {
  433. const char* const pname = carla_get_program_name(handle, fPluginId, i);
  434. if (pname[0] == '\0')
  435. continue;
  436. PluginGenericUI::Preset& preset(ui->presets[j]);
  437. preset.index = i;
  438. preset.name = strdup(pname);
  439. ui->presetStrings[j] = preset.name;
  440. ++j;
  441. }
  442. ui->currentPreset = -1;
  443. fPluginGenericUI = ui;
  444. }
  445. void updatePluginGenericUI(const CarlaHostHandle handle)
  446. {
  447. PluginGenericUI* const ui = fPluginGenericUI;
  448. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  449. for (uint32_t i=0; i < ui->parameterCount; ++i)
  450. {
  451. ui->values[i] = carla_get_current_parameter_value(handle, fPluginId, ui->parameters[i].rindex);
  452. if (ui->parameters[i].boolean)
  453. ui->parameters[i].bvalue = ui->values[i] > ui->parameters[i].min;
  454. }
  455. }
  456. bool loadPlugin(const CarlaHostHandle handle, const PluginInfoCache& info)
  457. {
  458. if (fPluginRunning || fPluginId != 0)
  459. {
  460. hidePluginUI(handle);
  461. carla_replace_plugin(handle, fPluginId);
  462. }
  463. carla_set_engine_option(handle, ENGINE_OPTION_PREFER_PLUGIN_BRIDGES, fPluginWillRunInBridgeMode, nullptr);
  464. const MutexLocker cml(fPlugin->sPluginInfoLoadMutex);
  465. const bool ok = carla_add_plugin(handle,
  466. info.btype,
  467. fPluginType,
  468. info.filename.c_str(),
  469. info.name.c_str(),
  470. info.label.c_str(),
  471. info.uniqueId,
  472. nullptr,
  473. PLUGIN_OPTIONS_NULL);
  474. if (ok)
  475. {
  476. d_debug("loadeded a plugin with label '%s' and name '%s' %lu",
  477. info.name.c_str(), info.label.c_str(), info.uniqueId);
  478. fPluginRunning = true;
  479. fPluginGenericUI = nullptr;
  480. fPluginFilename.clear();
  481. #ifdef DISTRHO_OS_MAC
  482. const bool brokenOffset = fPluginType == PLUGIN_VST2
  483. && info.name == "Renoise Redux"
  484. && info.uniqueId == d_cconst('R', 'R', 'D', 'X');
  485. fPluginHostWindow.setOffsetBroken(brokenOffset);
  486. #endif
  487. showPluginUI(handle, false);
  488. #ifdef WASM_TESTING
  489. d_stdout("loaded a plugin with label '%s'", label);
  490. if (std::strcmp(label, "audiofile") == 0)
  491. {
  492. d_stdout("Loading mp3 file into audiofile plugin");
  493. carla_set_custom_data(handle, fPluginId, CUSTOM_DATA_TYPE_PATH, "file", "/foolme.mp3");
  494. carla_set_parameter_value(handle, fPluginId, 1, 0.0f);
  495. fPluginGenericUI->values[1] = 0.0f;
  496. }
  497. #endif
  498. }
  499. else
  500. {
  501. fPopupError = carla_get_last_error(handle);
  502. d_stdout("got error: %s", fPopupError.buffer());
  503. fDrawingState = kDrawingPluginError;
  504. }
  505. repaint();
  506. return ok;
  507. }
  508. void loadFileAsPlugin(const CarlaHostHandle handle, const char* const filename)
  509. {
  510. if (fPluginRunning || fPluginId != 0)
  511. {
  512. hidePluginUI(handle);
  513. carla_replace_plugin(handle, fPluginId);
  514. }
  515. carla_set_engine_option(handle, ENGINE_OPTION_PREFER_PLUGIN_BRIDGES, fPluginWillRunInBridgeMode, nullptr);
  516. const MutexLocker cml(fPlugin->sPluginInfoLoadMutex);
  517. if (carla_load_file(handle, filename))
  518. {
  519. fPluginRunning = true;
  520. fPluginGenericUI = nullptr;
  521. fPluginFilename = filename;
  522. showPluginUI(handle, false);
  523. }
  524. else
  525. {
  526. fPopupError = carla_get_last_error(handle);
  527. d_stdout("got error: %s", fPopupError.buffer());
  528. fDrawingState = kDrawingPluginError;
  529. fPluginFilename.clear();
  530. }
  531. repaint();
  532. }
  533. protected:
  534. void pluginWindowResized(const uint width, const uint height) override
  535. {
  536. if (fIgnoreNextHostWindowResize)
  537. {
  538. fIgnoreNextHostWindowResize = false;
  539. return;
  540. }
  541. if (fShowingHostWindow)
  542. {
  543. fShowingHostWindow = false;
  544. fIgnoreNextHostWindowResize = true;
  545. fUpdateGeometryConstraints = true;
  546. }
  547. const uint extraHeight = kButtonHeight * getScaleFactor() + ImGui::GetStyle().WindowPadding.y * 2;
  548. fNextSize = Size<uint>(width, height + extraHeight);
  549. // reduce geometry constraint if needed
  550. if (fIgnoreNextHostWindowResize)
  551. return;
  552. if (width < fCurrentConstraintSize.getWidth() || height + extraHeight < fCurrentConstraintSize.getHeight())
  553. fUpdateGeometryConstraints = true;
  554. if (fPluginIsIdling && fLastSize != fNextSize)
  555. {
  556. fLastSize = fNextSize;
  557. if (fUpdateGeometryConstraints)
  558. {
  559. fUpdateGeometryConstraints = false;
  560. fCurrentConstraintSize = fNextSize;
  561. setGeometryConstraints(fNextSize.getWidth(), fNextSize.getHeight());
  562. }
  563. setSize(fNextSize);
  564. }
  565. }
  566. #if DISTRHO_UI_USER_RESIZABLE
  567. void onResize(const ResizeEvent& ev) override
  568. {
  569. UI::onResize(ev);
  570. if (fIgnoreNextHostWindowResize)
  571. return;
  572. if (fShowingHostWindow)
  573. return;
  574. if (fDrawingState != kDrawingPluginEmbedUI)
  575. return;
  576. const double scaleFactor = getScaleFactor();
  577. const uint extraHeight = kButtonHeight * scaleFactor + ImGui::GetStyle().WindowPadding.y * 2;
  578. uint width = ev.size.getWidth();
  579. uint height = ev.size.getHeight() - extraHeight;
  580. #ifdef DISTRHO_OS_MAC
  581. width /= scaleFactor;
  582. height /= scaleFactor;
  583. #endif
  584. fPluginHostWindow.setSize(width, height);
  585. }
  586. #endif
  587. void uiIdle() override
  588. {
  589. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  590. DISTRHO_SAFE_ASSERT_RETURN(handle != nullptr,);
  591. if (fDrawingState == kDrawingPluginGenericUI && fPluginGenericUI != nullptr && fPluginHasOutputParameters)
  592. {
  593. updatePluginGenericUI(handle);
  594. repaint();
  595. }
  596. if (fNextSize.isValid() && fLastSize != fNextSize)
  597. {
  598. fLastSize = fNextSize;
  599. if (fUpdateGeometryConstraints)
  600. {
  601. fUpdateGeometryConstraints = false;
  602. fCurrentConstraintSize = fNextSize;
  603. setGeometryConstraints(fNextSize.getWidth(), fNextSize.getHeight());
  604. }
  605. setSize(fNextSize);
  606. }
  607. switch (fIdleState)
  608. {
  609. case kIdleInit:
  610. fIdleState = kIdleNothing;
  611. initAndStartRunner();
  612. break;
  613. case kIdleInitPluginAlreadyLoaded:
  614. fIdleState = kIdleNothing;
  615. showPluginUI(handle, false);
  616. initAndStartRunner();
  617. break;
  618. case kIdlePluginLoadedFromDSP:
  619. fIdleState = kIdleNothing;
  620. showPluginUI(handle, false);
  621. break;
  622. case kIdleLoadSelectedPlugin:
  623. fIdleState = kIdleNothing;
  624. loadSelectedPlugin(handle);
  625. break;
  626. case kIdleResetPlugin:
  627. fIdleState = kIdleNothing;
  628. if (fPluginFilename.isNotEmpty())
  629. loadFileAsPlugin(handle, fPluginFilename.buffer());
  630. else
  631. loadPlugin(handle, fCurrentPluginInfo);
  632. break;
  633. case kIdleOpenFileUI:
  634. fIdleState = kIdleNothing;
  635. carla_show_custom_ui(handle, fPluginId, true);
  636. break;
  637. case kIdleShowCustomUI:
  638. fIdleState = kIdleNothing;
  639. showPluginUI(handle, true);
  640. break;
  641. case kIdleHideEmbedAndShowGenericUI:
  642. fIdleState = kIdleNothing;
  643. hidePluginUI(handle);
  644. createOrUpdatePluginGenericUI(handle);
  645. break;
  646. case kIdleHidePluginUI:
  647. fIdleState = kIdleNothing;
  648. hidePluginUI(handle);
  649. break;
  650. case kIdleGiveIdleToUI:
  651. if (fPlugin->fCarlaPluginDescriptor->ui_idle != nullptr)
  652. fPlugin->fCarlaPluginDescriptor->ui_idle(fPlugin->fCarlaPluginHandle);
  653. fPluginIsIdling = true;
  654. fPluginHostWindow.idle();
  655. fPluginIsIdling = false;
  656. break;
  657. case kIdleChangePluginType:
  658. fIdleState = kIdleNothing;
  659. if (fPluginRunning)
  660. hidePluginUI(handle);
  661. if (fNextPluginType == PLUGIN_TYPE_COUNT)
  662. {
  663. FileBrowserOptions opts;
  664. opts.title = "Load from file";
  665. openFileBrowser(opts);
  666. }
  667. else
  668. {
  669. fPluginSelected = -1;
  670. stopRunner();
  671. fPluginType = fNextPluginType;
  672. initAndStartRunner();
  673. }
  674. break;
  675. case kIdleNothing:
  676. break;
  677. }
  678. }
  679. void loadSelectedPlugin(const CarlaHostHandle handle)
  680. {
  681. DISTRHO_SAFE_ASSERT_RETURN(fPluginSelected >= 0,);
  682. PluginInfoCache info;
  683. {
  684. const MutexLocker cml(fPluginsMutex);
  685. info = fPlugins[fPluginSelected];
  686. }
  687. d_stdout("Loading %s...", info.name.c_str());
  688. if (loadPlugin(handle, info))
  689. fCurrentPluginInfo = info;
  690. }
  691. void uiFileBrowserSelected(const char* const filename) override
  692. {
  693. if (fPlugin != nullptr && fPlugin->fCarlaHostHandle != nullptr && filename != nullptr)
  694. {
  695. if (fNextPluginType == PLUGIN_TYPE_COUNT)
  696. loadFileAsPlugin(fPlugin->fCarlaHostHandle, filename);
  697. else
  698. carla_set_custom_data(fPlugin->fCarlaHostHandle, fPluginId, CUSTOM_DATA_TYPE_PATH, "file", filename);
  699. }
  700. }
  701. bool initAndStartRunner()
  702. {
  703. if (isRunnerActive())
  704. stopRunner();
  705. fRunnerData.init();
  706. return startRunner();
  707. }
  708. bool run() override
  709. {
  710. if (fRunnerData.needsReinit)
  711. {
  712. fRunnerData.needsReinit = false;
  713. {
  714. const MutexLocker cml(fPluginsMutex);
  715. fPlugins.clear();
  716. }
  717. d_stdout("Will scan plugins now...");
  718. const String& binaryPath(fPlugin->fBinaryPath);
  719. if (binaryPath.isNotEmpty())
  720. {
  721. fBinaryType = BINARY_NATIVE;
  722. fDiscoveryTool = binaryPath;
  723. fDiscoveryTool += DISTRHO_OS_SEP_STR "carla-discovery-native";
  724. #ifdef CARLA_OS_WIN
  725. fDiscoveryTool += ".exe";
  726. #endif
  727. fRunnerData.handle = carla_plugin_discovery_start(fDiscoveryTool,
  728. fBinaryType,
  729. fPluginType,
  730. IldaeilBasePlugin::getPluginPath(fPluginType),
  731. _binaryPluginSearchCallback,
  732. _binaryPluginCheckCacheCallback,
  733. this);
  734. }
  735. if (fDrawingState == kDrawingLoading)
  736. {
  737. fDrawingState = kDrawingPluginList;
  738. fPluginSearchFirstShow = true;
  739. }
  740. if (binaryPath.isEmpty() || (fRunnerData.handle == nullptr && !startNextDiscovery()))
  741. {
  742. d_stdout("Nothing found!");
  743. return false;
  744. }
  745. }
  746. DISTRHO_SAFE_ASSERT_RETURN(fRunnerData.handle != nullptr, false);
  747. if (carla_plugin_discovery_idle(fRunnerData.handle))
  748. return true;
  749. // stop here
  750. carla_plugin_discovery_stop(fRunnerData.handle);
  751. fRunnerData.handle = nullptr;
  752. if (startNextDiscovery())
  753. return true;
  754. d_stdout("Found %lu plugins!", (ulong)fPlugins.size());
  755. return false;
  756. }
  757. bool startNextDiscovery()
  758. {
  759. if (! setNextDiscoveryTool())
  760. return false;
  761. fRunnerData.handle = carla_plugin_discovery_start(fDiscoveryTool,
  762. fBinaryType,
  763. fPluginType,
  764. IldaeilBasePlugin::getPluginPath(fPluginType),
  765. _binaryPluginSearchCallback,
  766. _binaryPluginCheckCacheCallback,
  767. this);
  768. if (fRunnerData.handle == nullptr)
  769. return startNextDiscovery();
  770. return true;
  771. }
  772. bool setNextDiscoveryTool()
  773. {
  774. switch (fPluginType)
  775. {
  776. case PLUGIN_VST2:
  777. case PLUGIN_VST3:
  778. case PLUGIN_CLAP:
  779. break;
  780. default:
  781. return false;
  782. }
  783. #ifdef CARLA_OS_WIN
  784. #ifdef CARLA_OS_WIN64
  785. // look for win32 plugins on win64
  786. if (fBinaryType == BINARY_NATIVE)
  787. {
  788. fBinaryType = BINARY_WIN32;
  789. fDiscoveryTool = fPlugin->fBinaryPath;
  790. fDiscoveryTool += CARLA_OS_SEP_STR "carla-discovery-win32.exe";
  791. if (water::File(fDiscoveryTool.buffer()).existsAsFile())
  792. return true;
  793. }
  794. #endif
  795. // no other types to try
  796. return false;
  797. #else // CARLA_OS_WIN
  798. #ifndef CARLA_OS_MAC
  799. // try 32bit plugins on 64bit systems, skipping macOS where 32bit is no longer supported
  800. if (fBinaryType == BINARY_NATIVE)
  801. {
  802. fBinaryType = BINARY_POSIX32;
  803. fDiscoveryTool = fPlugin->fBinaryPath;
  804. fDiscoveryTool += CARLA_OS_SEP_STR "carla-discovery-posix32";
  805. if (water::File(fDiscoveryTool.buffer()).existsAsFile())
  806. return true;
  807. }
  808. #endif
  809. // try wine bridges
  810. #ifdef CARLA_OS_64BIT
  811. if (fBinaryType == BINARY_NATIVE || fBinaryType == BINARY_POSIX32)
  812. {
  813. fBinaryType = BINARY_WIN64;
  814. fDiscoveryTool = fPlugin->fBinaryPath;
  815. fDiscoveryTool += CARLA_OS_SEP_STR "carla-discovery-win64.exe";
  816. if (water::File(fDiscoveryTool.buffer()).existsAsFile())
  817. return true;
  818. }
  819. #endif
  820. if (fBinaryType != BINARY_WIN32)
  821. {
  822. fBinaryType = BINARY_WIN32;
  823. fDiscoveryTool = fPlugin->fBinaryPath;
  824. fDiscoveryTool += CARLA_OS_SEP_STR "carla-discovery-win32.exe";
  825. if (water::File(fDiscoveryTool.buffer()).existsAsFile())
  826. return true;
  827. }
  828. return false;
  829. #endif // CARLA_OS_WIN
  830. }
  831. void binaryPluginSearchCallback(const CarlaPluginDiscoveryInfo* const info, const char* const sha1sum)
  832. {
  833. // save plugin info into cache
  834. if (sha1sum != nullptr)
  835. {
  836. const water::String configDir(getSpecialDir(kSpecialDirConfigForPlugin));
  837. const water::File cacheFile((configDir + CARLA_OS_SEP_STR "cache" CARLA_OS_SEP_STR + sha1sum).toRawUTF8());
  838. if (cacheFile.create().ok())
  839. {
  840. water::FileOutputStream stream(cacheFile);
  841. if (stream.openedOk())
  842. {
  843. if (info != nullptr)
  844. {
  845. stream.writeString(getBinaryTypeAsString(info->btype));
  846. stream.writeString(getPluginTypeAsString(info->ptype));
  847. stream.writeString(info->filename);
  848. stream.writeString(info->label);
  849. stream.writeInt64(info->uniqueId);
  850. stream.writeString(info->metadata.name);
  851. stream.writeString(info->metadata.maker);
  852. stream.writeString(getPluginCategoryAsString(info->metadata.category));
  853. stream.writeInt(info->metadata.hints);
  854. stream.writeCompressedInt(info->io.audioIns);
  855. stream.writeCompressedInt(info->io.audioOuts);
  856. stream.writeCompressedInt(info->io.cvIns);
  857. stream.writeCompressedInt(info->io.cvOuts);
  858. stream.writeCompressedInt(info->io.midiIns);
  859. stream.writeCompressedInt(info->io.midiOuts);
  860. stream.writeCompressedInt(info->io.parameterIns);
  861. stream.writeCompressedInt(info->io.parameterOuts);
  862. }
  863. }
  864. else
  865. {
  866. d_stderr("Failed to write cache file for %s%s%s",
  867. getSpecialDir(kSpecialDirConfigForPlugin),
  868. CARLA_OS_SEP_STR "cache" CARLA_OS_SEP_STR,
  869. sha1sum);
  870. }
  871. }
  872. else
  873. {
  874. d_stderr("Failed to write cache file directories for %s%s%s",
  875. getSpecialDir(kSpecialDirConfigForPlugin),
  876. CARLA_OS_SEP_STR "cache" CARLA_OS_SEP_STR,
  877. sha1sum);
  878. }
  879. }
  880. if (info == nullptr)
  881. return;
  882. if (info->io.cvIns != 0 || info->io.cvOuts != 0)
  883. return;
  884. if (info->io.midiIns != 0 && info->io.midiIns != 1)
  885. return;
  886. if (info->io.midiOuts != 0 && info->io.midiOuts != 1)
  887. return;
  888. #if ILDAEIL_STANDALONE
  889. if (fPluginType == PLUGIN_INTERNAL)
  890. {
  891. if (std::strcmp(info->label, "audiogain") == 0)
  892. return;
  893. if (std::strcmp(info->label, "midichanfilter") == 0)
  894. return;
  895. if (std::strcmp(info->label, "midichannelize") == 0)
  896. return;
  897. }
  898. #elif DISTRHO_PLUGIN_IS_SYNTH
  899. if (info->io.midiIns != 1)
  900. return;
  901. if (info->io.audioOuts == 0)
  902. return;
  903. #elif DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  904. if ((info->io.midiIns != 1 && info->io.audioIns != 0 && info->io.audioOuts != 0) || info->io.midiOuts != 1)
  905. return;
  906. if (info->io.audioIns != 0 || info->io.audioOuts != 0)
  907. return;
  908. #else
  909. if (info->io.audioIns != 1 && info->io.audioIns != 2)
  910. return;
  911. if (info->io.audioOuts != 1 && info->io.audioOuts != 2)
  912. return;
  913. #endif
  914. if (fPluginType == PLUGIN_INTERNAL)
  915. {
  916. #if !ILDAEIL_STANDALONE
  917. if (std::strcmp(info->label, "audiogain_s") == 0)
  918. return;
  919. #endif
  920. if (std::strcmp(info->label, "lfo") == 0)
  921. return;
  922. if (std::strcmp(info->label, "midi2cv") == 0)
  923. return;
  924. if (std::strcmp(info->label, "midithrough") == 0)
  925. return;
  926. if (std::strcmp(info->label, "3bandsplitter") == 0)
  927. return;
  928. }
  929. const PluginInfoCache pinfo = {
  930. info->btype,
  931. info->uniqueId,
  932. info->filename,
  933. info->metadata.name,
  934. info->label,
  935. };
  936. const MutexLocker cml(fPluginsMutex);
  937. fPlugins.push_back(pinfo);
  938. }
  939. static void _binaryPluginSearchCallback(void* const ptr,
  940. const CarlaPluginDiscoveryInfo* const info,
  941. const char* const sha1sum)
  942. {
  943. static_cast<IldaeilUI*>(ptr)->binaryPluginSearchCallback(info, sha1sum);
  944. }
  945. bool binaryPluginCheckCacheCallback(const char* const filename, const char* const sha1sum)
  946. {
  947. if (sha1sum == nullptr)
  948. return false;
  949. const water::String configDir(getSpecialDir(kSpecialDirConfigForPlugin));
  950. const water::File cacheFile((configDir + CARLA_OS_SEP_STR "cache" CARLA_OS_SEP_STR + sha1sum).toRawUTF8());
  951. if (cacheFile.existsAsFile())
  952. {
  953. water::FileInputStream stream(cacheFile);
  954. if (stream.openedOk())
  955. {
  956. while (! stream.isExhausted())
  957. {
  958. CarlaPluginDiscoveryInfo info = {};
  959. // read back everything the same way and order as we wrote it
  960. info.btype = getBinaryTypeFromString(stream.readString().toRawUTF8());
  961. info.ptype = getPluginTypeFromString(stream.readString().toRawUTF8());
  962. const water::String pfilename(stream.readString());
  963. const water::String label(stream.readString());
  964. info.uniqueId = stream.readInt64();
  965. const water::String name(stream.readString());
  966. const water::String maker(stream.readString());
  967. info.metadata.category = getPluginCategoryFromString(stream.readString().toRawUTF8());
  968. info.metadata.hints = stream.readInt();
  969. info.io.audioIns = stream.readCompressedInt();
  970. info.io.audioOuts = stream.readCompressedInt();
  971. info.io.cvIns = stream.readCompressedInt();
  972. info.io.cvOuts = stream.readCompressedInt();
  973. info.io.midiIns = stream.readCompressedInt();
  974. info.io.midiOuts = stream.readCompressedInt();
  975. info.io.parameterIns = stream.readCompressedInt();
  976. info.io.parameterOuts = stream.readCompressedInt();
  977. // string stuff
  978. info.filename = pfilename.toRawUTF8();
  979. info.label = label.toRawUTF8();
  980. info.metadata.name = name.toRawUTF8();
  981. info.metadata.maker = maker.toRawUTF8();
  982. // check sha1 collisions
  983. if (pfilename != filename)
  984. {
  985. d_stderr("Cache hash collision for %s: \"%s\" vs \"%s\"",
  986. sha1sum, pfilename.toRawUTF8(), filename);
  987. return false;
  988. }
  989. // purposefully not passing sha1sum, to not override cache file
  990. binaryPluginSearchCallback(&info, nullptr);
  991. }
  992. return true;
  993. }
  994. else
  995. {
  996. d_stderr("Failed to read cache file for %s%s%s",
  997. getSpecialDir(kSpecialDirConfigForPlugin),
  998. CARLA_OS_SEP_STR "cache" CARLA_OS_SEP_STR,
  999. sha1sum);
  1000. }
  1001. }
  1002. return false;
  1003. }
  1004. static bool _binaryPluginCheckCacheCallback(void* const ptr, const char* const filename, const char* const sha1)
  1005. {
  1006. return static_cast<IldaeilUI*>(ptr)->binaryPluginCheckCacheCallback(filename, sha1);
  1007. }
  1008. void onImGuiDisplay() override
  1009. {
  1010. switch (fDrawingState)
  1011. {
  1012. case kDrawingLoading:
  1013. drawLoading();
  1014. break;
  1015. case kDrawingPluginError:
  1016. ImGui::OpenPopup("Plugin Error");
  1017. // call ourselves again with the plugin list
  1018. fDrawingState = kDrawingPluginList;
  1019. onImGuiDisplay();
  1020. break;
  1021. case kDrawingPluginList:
  1022. drawPluginList();
  1023. break;
  1024. case kDrawingPluginGenericUI:
  1025. drawTopBar();
  1026. drawGenericUI();
  1027. break;
  1028. case kDrawingPluginEmbedUI:
  1029. drawTopBar();
  1030. break;
  1031. case kDrawingErrorInit:
  1032. fDrawingState = kDrawingErrorDraw;
  1033. drawError(true);
  1034. break;
  1035. case kDrawingErrorDraw:
  1036. drawError(false);
  1037. break;
  1038. }
  1039. }
  1040. void drawError(const bool open)
  1041. {
  1042. ImGui::SetNextWindowPos(ImVec2(0, 0));
  1043. ImGui::SetNextWindowSize(ImVec2(getWidth(), getHeight()));
  1044. const int flags = ImGuiWindowFlags_NoSavedSettings
  1045. | ImGuiWindowFlags_NoTitleBar
  1046. | ImGuiWindowFlags_NoResize
  1047. | ImGuiWindowFlags_NoCollapse
  1048. | ImGuiWindowFlags_NoScrollbar
  1049. | ImGuiWindowFlags_NoScrollWithMouse;
  1050. if (ImGui::Begin("Error Window", nullptr, flags))
  1051. {
  1052. if (open)
  1053. ImGui::OpenPopup("Engine Error");
  1054. const int pflags = ImGuiWindowFlags_NoSavedSettings
  1055. | ImGuiWindowFlags_NoResize
  1056. | ImGuiWindowFlags_NoCollapse
  1057. | ImGuiWindowFlags_NoScrollbar
  1058. | ImGuiWindowFlags_NoScrollWithMouse
  1059. | ImGuiWindowFlags_AlwaysAutoResize
  1060. | ImGuiWindowFlags_AlwaysUseWindowPadding;
  1061. if (ImGui::BeginPopupModal("Engine Error", nullptr, pflags))
  1062. {
  1063. ImGui::TextUnformatted(fPopupError.buffer(), nullptr);
  1064. ImGui::EndPopup();
  1065. }
  1066. }
  1067. ImGui::End();
  1068. }
  1069. void drawTopBar()
  1070. {
  1071. const double scaleFactor = getScaleFactor();
  1072. const float padding = ImGui::GetStyle().WindowPadding.y * 2;
  1073. ImGui::SetNextWindowPos(ImVec2(0, 0));
  1074. ImGui::SetNextWindowSize(ImVec2(getWidth(), kButtonHeight * scaleFactor + padding));
  1075. const int flags = ImGuiWindowFlags_NoSavedSettings
  1076. | ImGuiWindowFlags_NoTitleBar
  1077. | ImGuiWindowFlags_NoResize
  1078. | ImGuiWindowFlags_NoCollapse
  1079. | ImGuiWindowFlags_NoScrollbar
  1080. | ImGuiWindowFlags_NoScrollWithMouse;
  1081. if (ImGui::Begin("Current Plugin", nullptr, flags))
  1082. {
  1083. if (ImGui::Button("Pick Another..."))
  1084. {
  1085. fIdleState = kIdleHidePluginUI;
  1086. fDrawingState = kDrawingPluginList;
  1087. #ifndef DISTRHO_OS_WASM
  1088. fNextSize = Size<uint>(kInitialWidth * scaleFactor, kInitialHeight * scaleFactor);
  1089. fLastSize = Size<uint>();
  1090. fUpdateGeometryConstraints = true;
  1091. #endif
  1092. }
  1093. ImGui::SameLine();
  1094. if (ImGui::Button("Reset"))
  1095. fIdleState = kIdleResetPlugin;
  1096. if (fDrawingState == kDrawingPluginGenericUI)
  1097. {
  1098. if (fPluginHasCustomUI)
  1099. {
  1100. ImGui::SameLine();
  1101. if (ImGui::Button("Show Custom GUI"))
  1102. fIdleState = kIdleShowCustomUI;
  1103. }
  1104. if (fPluginHasFileOpen)
  1105. {
  1106. ImGui::SameLine();
  1107. if (ImGui::Button("Open File..."))
  1108. fIdleState = kIdleOpenFileUI;
  1109. }
  1110. #ifdef WASM_TESTING
  1111. ImGui::SameLine();
  1112. ImGui::TextUnformatted(" Plugin to control:");
  1113. for (uint i=1; i<10; ++i)
  1114. {
  1115. char txt[8];
  1116. sprintf(txt, "%d", i);
  1117. ImGui::SameLine();
  1118. if (ImGui::Button(txt))
  1119. {
  1120. fPluginId = i;
  1121. fPluginGenericUI = nullptr;
  1122. fIdleState = kIdleHideEmbedAndShowGenericUI;
  1123. }
  1124. }
  1125. #endif
  1126. }
  1127. if (fDrawingState == kDrawingPluginEmbedUI)
  1128. {
  1129. ImGui::SameLine();
  1130. if (ImGui::Button("Show Generic GUI"))
  1131. fIdleState = kIdleHideEmbedAndShowGenericUI;
  1132. }
  1133. #if ILDAEIL_STANDALONE
  1134. if (isUsingNativeAudio())
  1135. {
  1136. ImGui::SameLine();
  1137. ImGui::Spacing();
  1138. ImGui::SameLine();
  1139. if (supportsAudioInput() && !isAudioInputEnabled() && ImGui::Button("Enable Input"))
  1140. requestAudioInput();
  1141. ImGui::SameLine();
  1142. if (supportsMIDI() && !isMIDIEnabled() && ImGui::Button("Enable MIDI"))
  1143. requestMIDI();
  1144. if (fDrawingState != kDrawingPluginEmbedUI && supportsBufferSizeChanges())
  1145. {
  1146. ImGui::SameLine();
  1147. ImGui::Spacing();
  1148. ImGui::SameLine();
  1149. ImGui::Text("Buffer Size:");
  1150. static constexpr uint bufferSizes_i[] = {
  1151. #ifndef DISTRHO_OS_WASM
  1152. 128,
  1153. #endif
  1154. 256, 512, 1024, 2048, 4096, 8192,
  1155. #ifdef DISTRHO_OS_WASM
  1156. 16384,
  1157. #endif
  1158. };
  1159. static constexpr const char* bufferSizes_s[] = {
  1160. #ifndef DISTRHO_OS_WASM
  1161. "128",
  1162. #endif
  1163. "256", "512", "1024", "2048", "4096", "8192",
  1164. #ifdef DISTRHO_OS_WASM
  1165. "16384",
  1166. #endif
  1167. };
  1168. uint buffersize = getBufferSize();
  1169. int current = -1;
  1170. for (uint i=0; i<ARRAY_SIZE(bufferSizes_i); ++i)
  1171. {
  1172. if (bufferSizes_i[i] == buffersize)
  1173. {
  1174. current = i;
  1175. break;
  1176. }
  1177. }
  1178. ImGui::SameLine();
  1179. if (ImGui::Combo("##buffersize", &current, bufferSizes_s, ARRAY_SIZE(bufferSizes_s)))
  1180. {
  1181. const uint next = bufferSizes_i[current];
  1182. d_stdout("requesting new buffer size: %u -> %u", buffersize, next);
  1183. requestBufferSizeChange(next);
  1184. }
  1185. }
  1186. }
  1187. #endif
  1188. }
  1189. ImGui::End();
  1190. }
  1191. void setupMainWindowPos()
  1192. {
  1193. const float scaleFactor = getScaleFactor();
  1194. float y = 0;
  1195. float height = getHeight();
  1196. if (fDrawingState == kDrawingPluginGenericUI)
  1197. {
  1198. y = kButtonHeight * scaleFactor + ImGui::GetStyle().WindowPadding.y * 2 - scaleFactor;
  1199. height -= y;
  1200. }
  1201. ImGui::SetNextWindowPos(ImVec2(0, y));
  1202. ImGui::SetNextWindowSize(ImVec2(getWidth(), height));
  1203. }
  1204. void drawGenericUI()
  1205. {
  1206. setupMainWindowPos();
  1207. PluginGenericUI* const ui = fPluginGenericUI;
  1208. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1209. const int pflags = ImGuiWindowFlags_NoSavedSettings
  1210. | ImGuiWindowFlags_NoResize
  1211. | ImGuiWindowFlags_NoCollapse
  1212. | ImGuiWindowFlags_AlwaysAutoResize;
  1213. if (ImGui::Begin(ui->title, nullptr, pflags))
  1214. {
  1215. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  1216. if (fPluginIsBridge)
  1217. {
  1218. const bool active = carla_get_internal_parameter_value(handle, 0, PARAMETER_ACTIVE) > 0.5f;
  1219. if (active)
  1220. {
  1221. ImGui::BeginDisabled();
  1222. ImGui::Button("Reload bridge");
  1223. ImGui::EndDisabled();
  1224. }
  1225. else
  1226. {
  1227. if (ImGui::Button("Reload bridge"))
  1228. carla_set_active(handle, 0, true);
  1229. }
  1230. }
  1231. if (ui->presetCount != 0)
  1232. {
  1233. ImGui::Text("Preset:");
  1234. ImGui::SameLine();
  1235. if (ImGui::Combo("##presets", &ui->currentPreset, ui->presetStrings, ui->presetCount))
  1236. {
  1237. PluginGenericUI::Preset& preset(ui->presets[ui->currentPreset]);
  1238. carla_set_program(handle, fPluginId, preset.index);
  1239. }
  1240. }
  1241. for (uint32_t i=0; i < ui->parameterCount; ++i)
  1242. {
  1243. PluginGenericUI::Parameter& param(ui->parameters[i]);
  1244. if (param.readonly)
  1245. {
  1246. ImGui::BeginDisabled();
  1247. ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.printformat,
  1248. ImGuiSliderFlags_NoInput | (param.log ? ImGuiSliderFlags_Logarithmic : 0x0));
  1249. ImGui::EndDisabled();
  1250. continue;
  1251. }
  1252. if (param.boolean)
  1253. {
  1254. if (ImGui::Checkbox(param.name, &ui->parameters[i].bvalue))
  1255. {
  1256. if (ImGui::IsItemActivated())
  1257. {
  1258. carla_set_parameter_touch(handle, fPluginId, param.rindex, true);
  1259. // editParameter(0, true);
  1260. }
  1261. ui->values[i] = ui->parameters[i].bvalue ? ui->parameters[i].max : ui->parameters[i].min;
  1262. carla_set_parameter_value(handle, fPluginId, param.rindex, ui->values[i]);
  1263. // setParameterValue(0, ui->values[i]);
  1264. }
  1265. }
  1266. else
  1267. {
  1268. const bool ret = param.log
  1269. ? ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.printformat, ImGuiSliderFlags_Logarithmic)
  1270. : ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.printformat);
  1271. if (ret)
  1272. {
  1273. if (ImGui::IsItemActivated())
  1274. {
  1275. carla_set_parameter_touch(handle, fPluginId, param.rindex, true);
  1276. // editParameter(0, true);
  1277. }
  1278. carla_set_parameter_value(handle, fPluginId, param.rindex, ui->values[i]);
  1279. // setParameterValue(0, ui->values[i]);
  1280. }
  1281. }
  1282. if (ImGui::IsItemDeactivated())
  1283. {
  1284. carla_set_parameter_touch(handle, fPluginId, param.rindex, false);
  1285. // editParameter(0, false);
  1286. }
  1287. }
  1288. }
  1289. ImGui::End();
  1290. }
  1291. void drawLoading()
  1292. {
  1293. setupMainWindowPos();
  1294. constexpr const int plflags = ImGuiWindowFlags_NoSavedSettings
  1295. | ImGuiWindowFlags_NoDecoration;
  1296. if (ImGui::Begin("Plugin List", nullptr, plflags))
  1297. ImGui::TextUnformatted("Loading...", nullptr);
  1298. ImGui::End();
  1299. }
  1300. void drawPluginList()
  1301. {
  1302. static const char* pluginTypes[] = {
  1303. getPluginTypeAsString(PLUGIN_INTERNAL),
  1304. getPluginTypeAsString(PLUGIN_LADSPA),
  1305. getPluginTypeAsString(PLUGIN_DSSI),
  1306. getPluginTypeAsString(PLUGIN_LV2),
  1307. getPluginTypeAsString(PLUGIN_VST2),
  1308. getPluginTypeAsString(PLUGIN_VST3),
  1309. getPluginTypeAsString(PLUGIN_CLAP),
  1310. getPluginTypeAsString(PLUGIN_JSFX),
  1311. "Load from file..."
  1312. };
  1313. setupMainWindowPos();
  1314. constexpr const int plflags = ImGuiWindowFlags_NoSavedSettings
  1315. | ImGuiWindowFlags_NoDecoration;
  1316. if (ImGui::Begin("Plugin List", nullptr, plflags))
  1317. {
  1318. constexpr const int errflags = ImGuiWindowFlags_NoSavedSettings
  1319. | ImGuiWindowFlags_NoResize
  1320. | ImGuiWindowFlags_AlwaysAutoResize
  1321. | ImGuiWindowFlags_NoCollapse
  1322. | ImGuiWindowFlags_NoScrollbar
  1323. | ImGuiWindowFlags_NoScrollWithMouse;
  1324. if (ImGui::BeginPopupModal("Plugin Error", nullptr, errflags))
  1325. {
  1326. ImGui::TextWrapped("Failed to load plugin, error was:\n%s", fPopupError.buffer());
  1327. ImGui::Separator();
  1328. if (ImGui::Button("Ok"))
  1329. ImGui::CloseCurrentPopup();
  1330. ImGui::SameLine();
  1331. ImGui::Dummy(ImVec2(500 * getScaleFactor(), 1));
  1332. ImGui::EndPopup();
  1333. }
  1334. else if (fPluginSearchFirstShow)
  1335. {
  1336. fPluginSearchFirstShow = false;
  1337. ImGui::SetKeyboardFocusHere();
  1338. }
  1339. if (ImGui::InputText("##pluginsearch", fPluginSearchString, sizeof(fPluginSearchString)-1,
  1340. ImGuiInputTextFlags_CharsNoBlank|ImGuiInputTextFlags_AutoSelectAll))
  1341. fPluginSearchActive = true;
  1342. if (ImGui::IsKeyDown(ImGuiKey_Escape))
  1343. fPluginSearchActive = false;
  1344. ImGui::SameLine();
  1345. ImGui::PushItemWidth(-1.0f);
  1346. int current;
  1347. switch (fPluginType)
  1348. {
  1349. case PLUGIN_JSFX: current = 7; break;
  1350. case PLUGIN_CLAP: current = 6; break;
  1351. case PLUGIN_VST3: current = 5; break;
  1352. case PLUGIN_VST2: current = 4; break;
  1353. case PLUGIN_LV2: current = 3; break;
  1354. case PLUGIN_DSSI: current = 2; break;
  1355. case PLUGIN_LADSPA: current = 1; break;
  1356. default: current = 0; break;
  1357. }
  1358. if (ImGui::Combo("##plugintypes", &current, pluginTypes, ARRAY_SIZE(pluginTypes)))
  1359. {
  1360. fIdleState = kIdleChangePluginType;
  1361. switch (current)
  1362. {
  1363. case 0: fNextPluginType = PLUGIN_INTERNAL; break;
  1364. case 1: fNextPluginType = PLUGIN_LADSPA; break;
  1365. case 2: fNextPluginType = PLUGIN_DSSI; break;
  1366. case 3: fNextPluginType = PLUGIN_LV2; break;
  1367. case 4: fNextPluginType = PLUGIN_VST2; break;
  1368. case 5: fNextPluginType = PLUGIN_VST3; break;
  1369. case 6: fNextPluginType = PLUGIN_CLAP; break;
  1370. case 7: fNextPluginType = PLUGIN_JSFX; break;
  1371. case 8: fNextPluginType = PLUGIN_TYPE_COUNT; break;
  1372. }
  1373. }
  1374. ImGui::BeginDisabled(fPluginSelected < 0);
  1375. if (ImGui::Button("Load Plugin"))
  1376. fIdleState = kIdleLoadSelectedPlugin;
  1377. // xx cardinal
  1378. if (fPluginType != PLUGIN_INTERNAL /*&& module->canUseBridges*/)
  1379. {
  1380. ImGui::SameLine();
  1381. ImGui::Checkbox("Run in bridge mode", &fPluginWillRunInBridgeMode);
  1382. }
  1383. ImGui::EndDisabled();
  1384. if (fPluginRunning)
  1385. {
  1386. ImGui::SameLine();
  1387. if (ImGui::Button("Cancel"))
  1388. fIdleState = kIdleShowCustomUI;
  1389. }
  1390. if (ImGui::BeginChild("pluginlistwindow"))
  1391. {
  1392. if (ImGui::BeginTable("pluginlist", 2, ImGuiTableFlags_NoSavedSettings))
  1393. {
  1394. const char* const search = fPluginSearchActive && fPluginSearchString[0] != '\0' ? fPluginSearchString : nullptr;
  1395. switch (fPluginType)
  1396. {
  1397. case PLUGIN_INTERNAL:
  1398. case PLUGIN_AU:
  1399. ImGui::TableSetupColumn("Name");
  1400. ImGui::TableSetupColumn("Label");
  1401. ImGui::TableHeadersRow();
  1402. break;
  1403. case PLUGIN_LV2:
  1404. ImGui::TableSetupColumn("Name");
  1405. ImGui::TableSetupColumn("URI");
  1406. ImGui::TableHeadersRow();
  1407. break;
  1408. default:
  1409. ImGui::TableSetupColumn("Name");
  1410. ImGui::TableSetupColumn("Filename");
  1411. ImGui::TableHeadersRow();
  1412. break;
  1413. }
  1414. const MutexLocker cml(fPluginsMutex);
  1415. for (uint i=0; i<fPlugins.size(); ++i)
  1416. {
  1417. const PluginInfoCache& info(fPlugins[i]);
  1418. if (search != nullptr && ildaeil::strcasestr(info.name.c_str(), search) == nullptr)
  1419. continue;
  1420. bool selected = fPluginSelected >= 0 && static_cast<uint>(fPluginSelected) == i;
  1421. switch (fPluginType)
  1422. {
  1423. case PLUGIN_INTERNAL:
  1424. case PLUGIN_AU:
  1425. ImGui::TableNextRow();
  1426. ImGui::TableSetColumnIndex(0);
  1427. ImGui::Selectable(info.name.c_str(), &selected);
  1428. ImGui::TableSetColumnIndex(1);
  1429. ImGui::Selectable(info.label.c_str(), &selected);
  1430. break;
  1431. case PLUGIN_LV2:
  1432. ImGui::TableNextRow();
  1433. ImGui::TableSetColumnIndex(0);
  1434. ImGui::Selectable(info.name.c_str(), &selected);
  1435. ImGui::TableSetColumnIndex(1);
  1436. ImGui::Selectable(info.label.c_str(), &selected);
  1437. break;
  1438. default:
  1439. ImGui::TableNextRow();
  1440. ImGui::TableSetColumnIndex(0);
  1441. ImGui::Selectable(info.name.c_str(), &selected);
  1442. ImGui::TableSetColumnIndex(1);
  1443. ImGui::Selectable(info.filename.c_str(), &selected);
  1444. break;
  1445. }
  1446. if (selected)
  1447. fPluginSelected = i;
  1448. }
  1449. ImGui::EndTable();
  1450. }
  1451. ImGui::EndChild();
  1452. }
  1453. }
  1454. ImGui::End();
  1455. }
  1456. protected:
  1457. /* --------------------------------------------------------------------------------------------------------
  1458. * DSP/Plugin Callbacks */
  1459. void parameterChanged(uint32_t, float) override
  1460. {
  1461. }
  1462. void stateChanged(const char* /* const key */, const char*) override
  1463. {
  1464. /*
  1465. if (std::strcmp(key, "project") == 0)
  1466. hidePluginUI(fPlugin->fCarlaHostHandle);
  1467. */
  1468. }
  1469. // -------------------------------------------------------------------------------------------------------
  1470. private:
  1471. /**
  1472. Set our UI class as non-copyable and add a leak detector just in case.
  1473. */
  1474. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(IldaeilUI)
  1475. };
  1476. // --------------------------------------------------------------------------------------------------------------------
  1477. void ildaeilProjectLoadedFromDSP(void* const ui)
  1478. {
  1479. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1480. static_cast<IldaeilUI*>(ui)->projectLoadedFromDSP();
  1481. }
  1482. void ildaeilParameterChangeForUI(void* const ui, const uint32_t index, const float value)
  1483. {
  1484. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1485. static_cast<IldaeilUI*>(ui)->changeParameterFromDSP(index, value);
  1486. }
  1487. void ildaeilResizeUI(void* const ui, const uint32_t width, const uint32_t height)
  1488. {
  1489. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1490. static_cast<IldaeilUI*>(ui)->resizeUI(width, height);
  1491. }
  1492. void ildaeilCloseUI(void* const ui)
  1493. {
  1494. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1495. static_cast<IldaeilUI*>(ui)->closeUI();
  1496. }
  1497. const char* ildaeilOpenFileForUI(void* const ui, const bool isDir, const char* const title, const char* const filter)
  1498. {
  1499. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr, nullptr);
  1500. return static_cast<IldaeilUI*>(ui)->openFileFromDSP(isDir, title, filter);
  1501. }
  1502. /* --------------------------------------------------------------------------------------------------------------------
  1503. * UI entry point, called by DPF to create a new UI instance. */
  1504. UI* createUI()
  1505. {
  1506. return new IldaeilUI();
  1507. }
  1508. // --------------------------------------------------------------------------------------------------------------------
  1509. END_NAMESPACE_DISTRHO