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.

1606 lines
53KB

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