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.

1611 lines
54KB

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