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.

1863 lines
62KB

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