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.

1747 lines
58KB

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