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.

1797 lines
59KB

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