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.

1802 lines
60KB

  1. /*
  2. * DISTRHO Ildaeil Plugin
  3. * Copyright (C) 2021-2024 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. fPluginHostWindow.setOffset(0, kButtonHeight * scaleFactor + paddingY);
  231. setGeometryConstraints(kInitialWidth * scaleFactor, kInitialWidth * scaleFactor);
  232. setSize(kInitialWidth * scaleFactor, kInitialHeight * scaleFactor);
  233. }
  234. else
  235. {
  236. fPluginHostWindow.setOffset(0, kButtonHeight + paddingY);
  237. setGeometryConstraints(kInitialWidth, kInitialWidth);
  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 + extraHeight < fCurrentConstraintSize.getHeight())
  573. fUpdateGeometryConstraints = true;
  574. }
  575. #if DISTRHO_UI_USER_RESIZABLE
  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. return;
  585. const double scaleFactor = getScaleFactor();
  586. const uint extraHeight = kButtonHeight * scaleFactor + ImGui::GetStyle().WindowPadding.y * 2;
  587. uint width = ev.size.getWidth();
  588. uint height = ev.size.getHeight() - extraHeight;
  589. #ifdef DISTRHO_OS_MAC
  590. width /= scaleFactor;
  591. height /= scaleFactor;
  592. #endif
  593. fPluginHostWindow.setSize(width, height);
  594. }
  595. #endif
  596. void uiIdle() override
  597. {
  598. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  599. DISTRHO_SAFE_ASSERT_RETURN(handle != nullptr,);
  600. if (fDrawingState == kDrawingPluginGenericUI && fPluginGenericUI != nullptr && fPluginHasOutputParameters)
  601. {
  602. updatePluginGenericUI(handle);
  603. repaint();
  604. }
  605. if (fNextSize.isValid() && fLastSize != fNextSize)
  606. {
  607. fLastSize = fNextSize;
  608. if (fUpdateGeometryConstraints)
  609. {
  610. fUpdateGeometryConstraints = false;
  611. fCurrentConstraintSize = fNextSize;
  612. setGeometryConstraints(fNextSize.getWidth(), fNextSize.getHeight());
  613. }
  614. setSize(fNextSize);
  615. }
  616. switch (fIdleState)
  617. {
  618. case kIdleInit:
  619. fIdleState = kIdleNothing;
  620. initAndStartRunner();
  621. break;
  622. case kIdleInitPluginAlreadyLoaded:
  623. fIdleState = kIdleNothing;
  624. showPluginUI(handle, false);
  625. initAndStartRunner();
  626. break;
  627. case kIdlePluginLoadedFromDSP:
  628. fIdleState = kIdleNothing;
  629. showPluginUI(handle, false);
  630. break;
  631. case kIdleLoadSelectedPlugin:
  632. fIdleState = kIdleNothing;
  633. loadSelectedPlugin(handle);
  634. break;
  635. case kIdleResetPlugin:
  636. fIdleState = kIdleNothing;
  637. if (fPluginFilename.isNotEmpty())
  638. loadFileAsPlugin(handle, fPluginFilename.buffer());
  639. else
  640. loadPlugin(handle, fCurrentPluginInfo);
  641. break;
  642. case kIdleOpenFileUI:
  643. fIdleState = kIdleNothing;
  644. carla_show_custom_ui(handle, fPluginId, true);
  645. break;
  646. case kIdleShowCustomUI:
  647. fIdleState = kIdleNothing;
  648. showPluginUI(handle, true);
  649. break;
  650. case kIdleHideEmbedAndShowGenericUI:
  651. fIdleState = kIdleNothing;
  652. hidePluginUI(handle);
  653. createOrUpdatePluginGenericUI(handle);
  654. break;
  655. case kIdleHidePluginUI:
  656. fIdleState = kIdleNothing;
  657. hidePluginUI(handle);
  658. break;
  659. case kIdleGiveIdleToUI:
  660. if (fPlugin->fCarlaPluginDescriptor->ui_idle != nullptr)
  661. fPlugin->fCarlaPluginDescriptor->ui_idle(fPlugin->fCarlaPluginHandle);
  662. fPluginHostWindow.idle();
  663. break;
  664. case kIdleChangePluginType:
  665. fIdleState = kIdleNothing;
  666. if (fPluginRunning)
  667. hidePluginUI(handle);
  668. if (fNextPluginType == PLUGIN_TYPE_COUNT)
  669. {
  670. FileBrowserOptions opts;
  671. opts.title = "Load from file";
  672. openFileBrowser(opts);
  673. }
  674. else
  675. {
  676. fPluginSelected = -1;
  677. stopRunner();
  678. fPluginType = fNextPluginType;
  679. initAndStartRunner();
  680. }
  681. break;
  682. case kIdleNothing:
  683. break;
  684. }
  685. }
  686. void loadSelectedPlugin(const CarlaHostHandle handle)
  687. {
  688. DISTRHO_SAFE_ASSERT_RETURN(fPluginSelected >= 0,);
  689. PluginInfoCache info;
  690. {
  691. const MutexLocker cml(fPluginsMutex);
  692. info = fPlugins[fPluginSelected];
  693. }
  694. d_stdout("Loading %s...", info.name.c_str());
  695. if (loadPlugin(handle, info))
  696. fCurrentPluginInfo = info;
  697. }
  698. void uiFileBrowserSelected(const char* const filename) override
  699. {
  700. if (fPlugin != nullptr && fPlugin->fCarlaHostHandle != nullptr && filename != nullptr)
  701. {
  702. if (fNextPluginType == PLUGIN_TYPE_COUNT)
  703. loadFileAsPlugin(fPlugin->fCarlaHostHandle, filename);
  704. else
  705. carla_set_custom_data(fPlugin->fCarlaHostHandle, fPluginId, CUSTOM_DATA_TYPE_PATH, "file", filename);
  706. }
  707. }
  708. bool initAndStartRunner()
  709. {
  710. if (isRunnerActive())
  711. stopRunner();
  712. fRunnerData.init();
  713. return startRunner();
  714. }
  715. bool run() override
  716. {
  717. if (fRunnerData.needsReinit)
  718. {
  719. fRunnerData.needsReinit = false;
  720. {
  721. const MutexLocker cml(fPluginsMutex);
  722. fPlugins.clear();
  723. }
  724. d_stdout("Will scan plugins now...");
  725. const String& binaryPath(fPlugin->fBinaryPath);
  726. if (binaryPath.isNotEmpty())
  727. {
  728. fBinaryType = BINARY_NATIVE;
  729. fDiscoveryTool = binaryPath;
  730. fDiscoveryTool += DISTRHO_OS_SEP_STR "carla-discovery-native";
  731. #ifdef CARLA_OS_WIN
  732. fDiscoveryTool += ".exe";
  733. #endif
  734. fRunnerData.handle = carla_plugin_discovery_start(fDiscoveryTool,
  735. fBinaryType,
  736. fPluginType,
  737. IldaeilBasePlugin::getPluginPath(fPluginType),
  738. _binaryPluginSearchCallback,
  739. _binaryPluginCheckCacheCallback,
  740. this);
  741. }
  742. if (fDrawingState == kDrawingLoading)
  743. {
  744. fDrawingState = kDrawingPluginList;
  745. fPluginSearchFirstShow = true;
  746. }
  747. if (binaryPath.isEmpty() || (fRunnerData.handle == nullptr && !startNextDiscovery()))
  748. {
  749. d_stdout("Nothing found!");
  750. return false;
  751. }
  752. }
  753. DISTRHO_SAFE_ASSERT_RETURN(fRunnerData.handle != nullptr, false);
  754. if (carla_plugin_discovery_idle(fRunnerData.handle))
  755. return true;
  756. // stop here
  757. carla_plugin_discovery_stop(fRunnerData.handle);
  758. fRunnerData.handle = nullptr;
  759. if (startNextDiscovery())
  760. return true;
  761. d_stdout("Found %lu plugins!", (ulong)fPlugins.size());
  762. return false;
  763. }
  764. bool startNextDiscovery()
  765. {
  766. if (! setNextDiscoveryTool())
  767. return false;
  768. fRunnerData.handle = carla_plugin_discovery_start(fDiscoveryTool,
  769. fBinaryType,
  770. fPluginType,
  771. IldaeilBasePlugin::getPluginPath(fPluginType),
  772. _binaryPluginSearchCallback,
  773. _binaryPluginCheckCacheCallback,
  774. this);
  775. if (fRunnerData.handle == nullptr)
  776. return startNextDiscovery();
  777. return true;
  778. }
  779. bool setNextDiscoveryTool()
  780. {
  781. switch (fPluginType)
  782. {
  783. case PLUGIN_VST2:
  784. case PLUGIN_VST3:
  785. case PLUGIN_CLAP:
  786. break;
  787. default:
  788. return false;
  789. }
  790. #ifdef CARLA_OS_WIN
  791. #ifdef CARLA_OS_WIN64
  792. // look for win32 plugins on win64
  793. if (fBinaryType == BINARY_NATIVE)
  794. {
  795. fBinaryType = BINARY_WIN32;
  796. fDiscoveryTool = fPlugin->fBinaryPath;
  797. fDiscoveryTool += CARLA_OS_SEP_STR "carla-discovery-win32.exe";
  798. if (water::File(fDiscoveryTool.buffer()).existsAsFile())
  799. return true;
  800. }
  801. #endif
  802. // no other types to try
  803. return false;
  804. #else // CARLA_OS_WIN
  805. #ifndef CARLA_OS_MAC
  806. // try 32bit plugins on 64bit systems, skipping macOS where 32bit is no longer supported
  807. if (fBinaryType == BINARY_NATIVE)
  808. {
  809. fBinaryType = BINARY_POSIX32;
  810. fDiscoveryTool = fPlugin->fBinaryPath;
  811. fDiscoveryTool += CARLA_OS_SEP_STR "carla-discovery-posix32";
  812. if (water::File(fDiscoveryTool.buffer()).existsAsFile())
  813. return true;
  814. }
  815. #endif
  816. // try wine bridges
  817. #ifdef CARLA_OS_64BIT
  818. if (fBinaryType == BINARY_NATIVE || fBinaryType == BINARY_POSIX32)
  819. {
  820. fBinaryType = BINARY_WIN64;
  821. fDiscoveryTool = fPlugin->fBinaryPath;
  822. fDiscoveryTool += CARLA_OS_SEP_STR "carla-discovery-win64.exe";
  823. if (water::File(fDiscoveryTool.buffer()).existsAsFile())
  824. return true;
  825. }
  826. #endif
  827. if (fBinaryType != BINARY_WIN32)
  828. {
  829. fBinaryType = BINARY_WIN32;
  830. fDiscoveryTool = fPlugin->fBinaryPath;
  831. fDiscoveryTool += CARLA_OS_SEP_STR "carla-discovery-win32.exe";
  832. if (water::File(fDiscoveryTool.buffer()).existsAsFile())
  833. return true;
  834. }
  835. return false;
  836. #endif // CARLA_OS_WIN
  837. }
  838. void binaryPluginSearchCallback(const CarlaPluginDiscoveryInfo* const info, const char* const sha1sum)
  839. {
  840. // save plugin info into cache
  841. if (sha1sum != nullptr)
  842. {
  843. const water::String configDir(ildaeilConfigDir());
  844. const water::File cacheFile(configDir + CARLA_OS_SEP_STR "cache" CARLA_OS_SEP_STR + sha1sum);
  845. if (cacheFile.create().ok())
  846. {
  847. water::FileOutputStream stream(cacheFile);
  848. if (stream.openedOk())
  849. {
  850. if (info != nullptr)
  851. {
  852. stream.writeString(getBinaryTypeAsString(info->btype));
  853. stream.writeString(getPluginTypeAsString(info->ptype));
  854. stream.writeString(info->filename);
  855. stream.writeString(info->label);
  856. stream.writeInt64(info->uniqueId);
  857. stream.writeString(info->metadata.name);
  858. stream.writeString(info->metadata.maker);
  859. stream.writeString(getPluginCategoryAsString(info->metadata.category));
  860. stream.writeInt(info->metadata.hints);
  861. stream.writeCompressedInt(info->io.audioIns);
  862. stream.writeCompressedInt(info->io.audioOuts);
  863. stream.writeCompressedInt(info->io.cvIns);
  864. stream.writeCompressedInt(info->io.cvOuts);
  865. stream.writeCompressedInt(info->io.midiIns);
  866. stream.writeCompressedInt(info->io.midiOuts);
  867. stream.writeCompressedInt(info->io.parameterIns);
  868. stream.writeCompressedInt(info->io.parameterOuts);
  869. }
  870. }
  871. else
  872. {
  873. d_stderr("Failed to write cache file for %s%s%s",
  874. ildaeilConfigDir(), CARLA_OS_SEP_STR "cache" CARLA_OS_SEP_STR, sha1sum);
  875. }
  876. }
  877. else
  878. {
  879. d_stderr("Failed to write cache file directories for %s%s%s",
  880. ildaeilConfigDir(), CARLA_OS_SEP_STR "cache" CARLA_OS_SEP_STR, sha1sum);
  881. }
  882. }
  883. if (info == nullptr)
  884. return;
  885. if (info->io.cvIns != 0 || info->io.cvOuts != 0)
  886. return;
  887. if (info->io.midiIns != 0 && info->io.midiIns != 1)
  888. return;
  889. if (info->io.midiOuts != 0 && info->io.midiOuts != 1)
  890. return;
  891. #if ILDAEIL_STANDALONE
  892. if (fPluginType == PLUGIN_INTERNAL)
  893. {
  894. if (std::strcmp(info->label, "audiogain") == 0)
  895. return;
  896. if (std::strcmp(info->label, "midichanfilter") == 0)
  897. return;
  898. if (std::strcmp(info->label, "midichannelize") == 0)
  899. return;
  900. }
  901. #elif DISTRHO_PLUGIN_IS_SYNTH
  902. if (info->io.midiIns != 1)
  903. return;
  904. if (info->io.audioOuts == 0)
  905. return;
  906. #elif DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  907. if ((info->io.midiIns != 1 && info->io.audioIns != 0 && info->io.audioOuts != 0) || info->io.midiOuts != 1)
  908. return;
  909. if (info->io.audioIns != 0 || info->io.audioOuts != 0)
  910. return;
  911. #else
  912. if (info->io.audioIns != 1 && info->io.audioIns != 2)
  913. return;
  914. if (info->io.audioOuts != 1 && info->io.audioOuts != 2)
  915. return;
  916. #endif
  917. if (fPluginType == PLUGIN_INTERNAL)
  918. {
  919. #if !ILDAEIL_STANDALONE
  920. if (std::strcmp(info->label, "audiogain_s") == 0)
  921. return;
  922. #endif
  923. if (std::strcmp(info->label, "lfo") == 0)
  924. return;
  925. if (std::strcmp(info->label, "midi2cv") == 0)
  926. return;
  927. if (std::strcmp(info->label, "midithrough") == 0)
  928. return;
  929. if (std::strcmp(info->label, "3bandsplitter") == 0)
  930. return;
  931. }
  932. const PluginInfoCache pinfo = {
  933. info->btype,
  934. info->uniqueId,
  935. info->filename,
  936. info->metadata.name,
  937. info->label,
  938. };
  939. const MutexLocker cml(fPluginsMutex);
  940. fPlugins.push_back(pinfo);
  941. }
  942. static void _binaryPluginSearchCallback(void* const ptr,
  943. const CarlaPluginDiscoveryInfo* const info,
  944. const char* const sha1sum)
  945. {
  946. static_cast<IldaeilUI*>(ptr)->binaryPluginSearchCallback(info, sha1sum);
  947. }
  948. bool binaryPluginCheckCacheCallback(const char* const filename, const char* const sha1sum)
  949. {
  950. if (sha1sum == nullptr)
  951. return false;
  952. const water::String configDir(ildaeilConfigDir());
  953. const water::File cacheFile(configDir + CARLA_OS_SEP_STR "cache" CARLA_OS_SEP_STR + sha1sum);
  954. if (cacheFile.existsAsFile())
  955. {
  956. water::FileInputStream stream(cacheFile);
  957. if (stream.openedOk())
  958. {
  959. while (! stream.isExhausted())
  960. {
  961. CarlaPluginDiscoveryInfo info = {};
  962. // read back everything the same way and order as we wrote it
  963. info.btype = getBinaryTypeFromString(stream.readString().toRawUTF8());
  964. info.ptype = getPluginTypeFromString(stream.readString().toRawUTF8());
  965. const water::String pfilename(stream.readString());
  966. const water::String label(stream.readString());
  967. info.uniqueId = stream.readInt64();
  968. const water::String name(stream.readString());
  969. const water::String maker(stream.readString());
  970. info.metadata.category = getPluginCategoryFromString(stream.readString().toRawUTF8());
  971. info.metadata.hints = stream.readInt();
  972. info.io.audioIns = stream.readCompressedInt();
  973. info.io.audioOuts = stream.readCompressedInt();
  974. info.io.cvIns = stream.readCompressedInt();
  975. info.io.cvOuts = stream.readCompressedInt();
  976. info.io.midiIns = stream.readCompressedInt();
  977. info.io.midiOuts = stream.readCompressedInt();
  978. info.io.parameterIns = stream.readCompressedInt();
  979. info.io.parameterOuts = stream.readCompressedInt();
  980. // string stuff
  981. info.filename = pfilename.toRawUTF8();
  982. info.label = label.toRawUTF8();
  983. info.metadata.name = name.toRawUTF8();
  984. info.metadata.maker = maker.toRawUTF8();
  985. // check sha1 collisions
  986. if (pfilename != filename)
  987. {
  988. d_stderr("Cache hash collision for %s: \"%s\" vs \"%s\"",
  989. sha1sum, pfilename.toRawUTF8(), filename);
  990. return false;
  991. }
  992. // purposefully not passing sha1sum, to not override cache file
  993. binaryPluginSearchCallback(&info, nullptr);
  994. }
  995. return true;
  996. }
  997. else
  998. {
  999. d_stderr("Failed to read cache file for %s%s%s",
  1000. ildaeilConfigDir(), CARLA_OS_SEP_STR "cache" CARLA_OS_SEP_STR, sha1sum);
  1001. }
  1002. }
  1003. return false;
  1004. }
  1005. static bool _binaryPluginCheckCacheCallback(void* const ptr, const char* const filename, const char* const sha1)
  1006. {
  1007. return static_cast<IldaeilUI*>(ptr)->binaryPluginCheckCacheCallback(filename, sha1);
  1008. }
  1009. void onImGuiDisplay() override
  1010. {
  1011. switch (fDrawingState)
  1012. {
  1013. case kDrawingLoading:
  1014. drawLoading();
  1015. break;
  1016. case kDrawingPluginError:
  1017. ImGui::OpenPopup("Plugin Error");
  1018. // call ourselves again with the plugin list
  1019. fDrawingState = kDrawingPluginList;
  1020. onImGuiDisplay();
  1021. break;
  1022. case kDrawingPluginList:
  1023. drawPluginList();
  1024. break;
  1025. case kDrawingPluginGenericUI:
  1026. drawTopBar();
  1027. drawGenericUI();
  1028. break;
  1029. case kDrawingPluginEmbedUI:
  1030. drawTopBar();
  1031. break;
  1032. case kDrawingErrorInit:
  1033. fDrawingState = kDrawingErrorDraw;
  1034. drawError(true);
  1035. break;
  1036. case kDrawingErrorDraw:
  1037. drawError(false);
  1038. break;
  1039. }
  1040. }
  1041. void drawError(const bool open)
  1042. {
  1043. ImGui::SetNextWindowPos(ImVec2(0, 0));
  1044. ImGui::SetNextWindowSize(ImVec2(getWidth(), getHeight()));
  1045. const int flags = ImGuiWindowFlags_NoSavedSettings
  1046. | ImGuiWindowFlags_NoTitleBar
  1047. | ImGuiWindowFlags_NoResize
  1048. | ImGuiWindowFlags_NoCollapse
  1049. | ImGuiWindowFlags_NoScrollbar
  1050. | ImGuiWindowFlags_NoScrollWithMouse;
  1051. if (ImGui::Begin("Error Window", nullptr, flags))
  1052. {
  1053. if (open)
  1054. ImGui::OpenPopup("Engine Error");
  1055. const int pflags = ImGuiWindowFlags_NoSavedSettings
  1056. | ImGuiWindowFlags_NoResize
  1057. | ImGuiWindowFlags_NoCollapse
  1058. | ImGuiWindowFlags_NoScrollbar
  1059. | ImGuiWindowFlags_NoScrollWithMouse
  1060. | ImGuiWindowFlags_AlwaysAutoResize
  1061. | ImGuiWindowFlags_AlwaysUseWindowPadding;
  1062. if (ImGui::BeginPopupModal("Engine Error", nullptr, pflags))
  1063. {
  1064. ImGui::TextUnformatted(fPopupError.buffer(), nullptr);
  1065. ImGui::EndPopup();
  1066. }
  1067. }
  1068. ImGui::End();
  1069. }
  1070. void drawTopBar()
  1071. {
  1072. const double scaleFactor = getScaleFactor();
  1073. const float padding = ImGui::GetStyle().WindowPadding.y * 2;
  1074. ImGui::SetNextWindowPos(ImVec2(0, 0));
  1075. ImGui::SetNextWindowSize(ImVec2(getWidth(), kButtonHeight * scaleFactor + padding));
  1076. const int flags = ImGuiWindowFlags_NoSavedSettings
  1077. | ImGuiWindowFlags_NoTitleBar
  1078. | ImGuiWindowFlags_NoResize
  1079. | ImGuiWindowFlags_NoCollapse
  1080. | ImGuiWindowFlags_NoScrollbar
  1081. | ImGuiWindowFlags_NoScrollWithMouse;
  1082. if (ImGui::Begin("Current Plugin", nullptr, flags))
  1083. {
  1084. if (ImGui::Button("Pick Another..."))
  1085. {
  1086. fIdleState = kIdleHidePluginUI;
  1087. fDrawingState = kDrawingPluginList;
  1088. #ifndef DISTRHO_OS_WASM
  1089. fNextSize = Size<uint>(kInitialWidth * scaleFactor, kInitialHeight * scaleFactor);
  1090. fLastSize = Size<uint>();
  1091. fUpdateGeometryConstraints = true;
  1092. #endif
  1093. }
  1094. ImGui::SameLine();
  1095. if (ImGui::Button("Reset"))
  1096. fIdleState = kIdleResetPlugin;
  1097. if (fDrawingState == kDrawingPluginGenericUI)
  1098. {
  1099. if (fPluginHasCustomUI)
  1100. {
  1101. ImGui::SameLine();
  1102. if (ImGui::Button("Show Custom GUI"))
  1103. fIdleState = kIdleShowCustomUI;
  1104. }
  1105. if (fPluginHasFileOpen)
  1106. {
  1107. ImGui::SameLine();
  1108. if (ImGui::Button("Open File..."))
  1109. fIdleState = kIdleOpenFileUI;
  1110. }
  1111. #ifdef WASM_TESTING
  1112. ImGui::SameLine();
  1113. ImGui::TextUnformatted(" Plugin to control:");
  1114. for (uint i=1; i<10; ++i)
  1115. {
  1116. char txt[8];
  1117. sprintf(txt, "%d", i);
  1118. ImGui::SameLine();
  1119. if (ImGui::Button(txt))
  1120. {
  1121. fPluginId = i;
  1122. fPluginGenericUI = nullptr;
  1123. fIdleState = kIdleHideEmbedAndShowGenericUI;
  1124. }
  1125. }
  1126. #endif
  1127. }
  1128. if (fDrawingState == kDrawingPluginEmbedUI)
  1129. {
  1130. ImGui::SameLine();
  1131. if (ImGui::Button("Show Generic GUI"))
  1132. fIdleState = kIdleHideEmbedAndShowGenericUI;
  1133. }
  1134. #if ILDAEIL_STANDALONE
  1135. if (isUsingNativeAudio())
  1136. {
  1137. ImGui::SameLine();
  1138. ImGui::Spacing();
  1139. ImGui::SameLine();
  1140. if (supportsAudioInput() && !isAudioInputEnabled() && ImGui::Button("Enable Input"))
  1141. requestAudioInput();
  1142. ImGui::SameLine();
  1143. if (supportsMIDI() && !isMIDIEnabled() && ImGui::Button("Enable MIDI"))
  1144. requestMIDI();
  1145. if (fDrawingState != kDrawingPluginEmbedUI && supportsBufferSizeChanges())
  1146. {
  1147. ImGui::SameLine();
  1148. ImGui::Spacing();
  1149. ImGui::SameLine();
  1150. ImGui::Text("Buffer Size:");
  1151. static constexpr uint bufferSizes_i[] = {
  1152. #ifndef DISTRHO_OS_WASM
  1153. 128,
  1154. #endif
  1155. 256, 512, 1024, 2048, 4096, 8192,
  1156. #ifdef DISTRHO_OS_WASM
  1157. 16384,
  1158. #endif
  1159. };
  1160. static constexpr const char* bufferSizes_s[] = {
  1161. #ifndef DISTRHO_OS_WASM
  1162. "128",
  1163. #endif
  1164. "256", "512", "1024", "2048", "4096", "8192",
  1165. #ifdef DISTRHO_OS_WASM
  1166. "16384",
  1167. #endif
  1168. };
  1169. uint buffersize = getBufferSize();
  1170. int current = -1;
  1171. for (uint i=0; i<ARRAY_SIZE(bufferSizes_i); ++i)
  1172. {
  1173. if (bufferSizes_i[i] == buffersize)
  1174. {
  1175. current = i;
  1176. break;
  1177. }
  1178. }
  1179. ImGui::SameLine();
  1180. if (ImGui::Combo("##buffersize", &current, bufferSizes_s, ARRAY_SIZE(bufferSizes_s)))
  1181. {
  1182. const uint next = bufferSizes_i[current];
  1183. d_stdout("requesting new buffer size: %u -> %u", buffersize, next);
  1184. requestBufferSizeChange(next);
  1185. }
  1186. }
  1187. }
  1188. #endif
  1189. }
  1190. ImGui::End();
  1191. }
  1192. void setupMainWindowPos()
  1193. {
  1194. const float scaleFactor = getScaleFactor();
  1195. float y = 0;
  1196. float height = getHeight();
  1197. if (fDrawingState == kDrawingPluginGenericUI)
  1198. {
  1199. y = kButtonHeight * scaleFactor + ImGui::GetStyle().WindowPadding.y * 2 - scaleFactor;
  1200. height -= y;
  1201. }
  1202. ImGui::SetNextWindowPos(ImVec2(0, y));
  1203. ImGui::SetNextWindowSize(ImVec2(getWidth(), height));
  1204. }
  1205. void drawGenericUI()
  1206. {
  1207. setupMainWindowPos();
  1208. PluginGenericUI* const ui = fPluginGenericUI;
  1209. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1210. const int pflags = ImGuiWindowFlags_NoSavedSettings
  1211. | ImGuiWindowFlags_NoResize
  1212. | ImGuiWindowFlags_NoCollapse
  1213. | ImGuiWindowFlags_AlwaysAutoResize;
  1214. if (ImGui::Begin(ui->title, nullptr, pflags))
  1215. {
  1216. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  1217. if (fPluginIsBridge)
  1218. {
  1219. const bool active = carla_get_internal_parameter_value(handle, 0, PARAMETER_ACTIVE) > 0.5f;
  1220. if (active)
  1221. {
  1222. ImGui::BeginDisabled();
  1223. ImGui::Button("Reload bridge");
  1224. ImGui::EndDisabled();
  1225. }
  1226. else
  1227. {
  1228. if (ImGui::Button("Reload bridge"))
  1229. carla_set_active(handle, 0, true);
  1230. }
  1231. }
  1232. if (ui->presetCount != 0)
  1233. {
  1234. ImGui::Text("Preset:");
  1235. ImGui::SameLine();
  1236. if (ImGui::Combo("##presets", &ui->currentPreset, ui->presetStrings, ui->presetCount))
  1237. {
  1238. PluginGenericUI::Preset& preset(ui->presets[ui->currentPreset]);
  1239. carla_set_program(handle, fPluginId, preset.index);
  1240. }
  1241. }
  1242. for (uint32_t i=0; i < ui->parameterCount; ++i)
  1243. {
  1244. PluginGenericUI::Parameter& param(ui->parameters[i]);
  1245. if (param.readonly)
  1246. {
  1247. ImGui::BeginDisabled();
  1248. ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.printformat,
  1249. ImGuiSliderFlags_NoInput | (param.log ? ImGuiSliderFlags_Logarithmic : 0x0));
  1250. ImGui::EndDisabled();
  1251. continue;
  1252. }
  1253. if (param.boolean)
  1254. {
  1255. if (ImGui::Checkbox(param.name, &ui->parameters[i].bvalue))
  1256. {
  1257. if (ImGui::IsItemActivated())
  1258. {
  1259. carla_set_parameter_touch(handle, fPluginId, param.rindex, true);
  1260. // editParameter(0, true);
  1261. }
  1262. ui->values[i] = ui->parameters[i].bvalue ? ui->parameters[i].max : ui->parameters[i].min;
  1263. carla_set_parameter_value(handle, fPluginId, param.rindex, ui->values[i]);
  1264. // setParameterValue(0, ui->values[i]);
  1265. }
  1266. }
  1267. else
  1268. {
  1269. const bool ret = param.log
  1270. ? ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.printformat, ImGuiSliderFlags_Logarithmic)
  1271. : ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.printformat);
  1272. if (ret)
  1273. {
  1274. if (ImGui::IsItemActivated())
  1275. {
  1276. carla_set_parameter_touch(handle, fPluginId, param.rindex, true);
  1277. // editParameter(0, true);
  1278. }
  1279. carla_set_parameter_value(handle, fPluginId, param.rindex, ui->values[i]);
  1280. // setParameterValue(0, ui->values[i]);
  1281. }
  1282. }
  1283. if (ImGui::IsItemDeactivated())
  1284. {
  1285. carla_set_parameter_touch(handle, fPluginId, param.rindex, false);
  1286. // editParameter(0, false);
  1287. }
  1288. }
  1289. }
  1290. ImGui::End();
  1291. }
  1292. void drawLoading()
  1293. {
  1294. setupMainWindowPos();
  1295. constexpr const int plflags = ImGuiWindowFlags_NoSavedSettings
  1296. | ImGuiWindowFlags_NoDecoration;
  1297. if (ImGui::Begin("Plugin List", nullptr, plflags))
  1298. ImGui::TextUnformatted("Loading...", nullptr);
  1299. ImGui::End();
  1300. }
  1301. void drawPluginList()
  1302. {
  1303. static const char* pluginTypes[] = {
  1304. getPluginTypeAsString(PLUGIN_INTERNAL),
  1305. getPluginTypeAsString(PLUGIN_LADSPA),
  1306. getPluginTypeAsString(PLUGIN_DSSI),
  1307. getPluginTypeAsString(PLUGIN_LV2),
  1308. getPluginTypeAsString(PLUGIN_VST2),
  1309. getPluginTypeAsString(PLUGIN_VST3),
  1310. getPluginTypeAsString(PLUGIN_CLAP),
  1311. getPluginTypeAsString(PLUGIN_JSFX),
  1312. "Load from file..."
  1313. };
  1314. setupMainWindowPos();
  1315. constexpr const int plflags = ImGuiWindowFlags_NoSavedSettings
  1316. | ImGuiWindowFlags_NoDecoration;
  1317. if (ImGui::Begin("Plugin List", nullptr, plflags))
  1318. {
  1319. constexpr const int errflags = ImGuiWindowFlags_NoSavedSettings
  1320. | ImGuiWindowFlags_NoResize
  1321. | ImGuiWindowFlags_AlwaysAutoResize
  1322. | ImGuiWindowFlags_NoCollapse
  1323. | ImGuiWindowFlags_NoScrollbar
  1324. | ImGuiWindowFlags_NoScrollWithMouse;
  1325. if (ImGui::BeginPopupModal("Plugin Error", nullptr, errflags))
  1326. {
  1327. ImGui::TextWrapped("Failed to load plugin, error was:\n%s", fPopupError.buffer());
  1328. ImGui::Separator();
  1329. if (ImGui::Button("Ok"))
  1330. ImGui::CloseCurrentPopup();
  1331. ImGui::SameLine();
  1332. ImGui::Dummy(ImVec2(500 * getScaleFactor(), 1));
  1333. ImGui::EndPopup();
  1334. }
  1335. else if (fPluginSearchFirstShow)
  1336. {
  1337. fPluginSearchFirstShow = false;
  1338. ImGui::SetKeyboardFocusHere();
  1339. }
  1340. if (ImGui::InputText("##pluginsearch", fPluginSearchString, sizeof(fPluginSearchString)-1,
  1341. ImGuiInputTextFlags_CharsNoBlank|ImGuiInputTextFlags_AutoSelectAll))
  1342. fPluginSearchActive = true;
  1343. if (ImGui::IsKeyDown(ImGuiKey_Escape))
  1344. fPluginSearchActive = false;
  1345. ImGui::SameLine();
  1346. ImGui::PushItemWidth(-1.0f);
  1347. int current;
  1348. switch (fPluginType)
  1349. {
  1350. case PLUGIN_JSFX: current = 7; break;
  1351. case PLUGIN_CLAP: current = 6; break;
  1352. case PLUGIN_VST3: current = 5; break;
  1353. case PLUGIN_VST2: current = 4; break;
  1354. case PLUGIN_LV2: current = 3; break;
  1355. case PLUGIN_DSSI: current = 2; break;
  1356. case PLUGIN_LADSPA: current = 1; break;
  1357. default: current = 0; break;
  1358. }
  1359. if (ImGui::Combo("##plugintypes", &current, pluginTypes, ARRAY_SIZE(pluginTypes)))
  1360. {
  1361. fIdleState = kIdleChangePluginType;
  1362. switch (current)
  1363. {
  1364. case 0: fNextPluginType = PLUGIN_INTERNAL; break;
  1365. case 1: fNextPluginType = PLUGIN_LADSPA; break;
  1366. case 2: fNextPluginType = PLUGIN_DSSI; break;
  1367. case 3: fNextPluginType = PLUGIN_LV2; break;
  1368. case 4: fNextPluginType = PLUGIN_VST2; break;
  1369. case 5: fNextPluginType = PLUGIN_VST3; break;
  1370. case 6: fNextPluginType = PLUGIN_CLAP; break;
  1371. case 7: fNextPluginType = PLUGIN_JSFX; break;
  1372. case 8: fNextPluginType = PLUGIN_TYPE_COUNT; break;
  1373. }
  1374. }
  1375. ImGui::BeginDisabled(fPluginSelected < 0);
  1376. if (ImGui::Button("Load Plugin"))
  1377. fIdleState = kIdleLoadSelectedPlugin;
  1378. // xx cardinal
  1379. if (fPluginType != PLUGIN_INTERNAL /*&& module->canUseBridges*/)
  1380. {
  1381. ImGui::SameLine();
  1382. ImGui::Checkbox("Run in bridge mode", &fPluginWillRunInBridgeMode);
  1383. }
  1384. ImGui::EndDisabled();
  1385. if (fPluginRunning)
  1386. {
  1387. ImGui::SameLine();
  1388. if (ImGui::Button("Cancel"))
  1389. fIdleState = kIdleShowCustomUI;
  1390. }
  1391. if (ImGui::BeginChild("pluginlistwindow"))
  1392. {
  1393. if (ImGui::BeginTable("pluginlist", 2, ImGuiTableFlags_NoSavedSettings))
  1394. {
  1395. const char* const search = fPluginSearchActive && fPluginSearchString[0] != '\0' ? fPluginSearchString : nullptr;
  1396. switch (fPluginType)
  1397. {
  1398. case PLUGIN_INTERNAL:
  1399. case PLUGIN_AU:
  1400. ImGui::TableSetupColumn("Name");
  1401. ImGui::TableSetupColumn("Label");
  1402. ImGui::TableHeadersRow();
  1403. break;
  1404. case PLUGIN_LV2:
  1405. ImGui::TableSetupColumn("Name");
  1406. ImGui::TableSetupColumn("URI");
  1407. ImGui::TableHeadersRow();
  1408. break;
  1409. default:
  1410. ImGui::TableSetupColumn("Name");
  1411. ImGui::TableSetupColumn("Filename");
  1412. ImGui::TableHeadersRow();
  1413. break;
  1414. }
  1415. const MutexLocker cml(fPluginsMutex);
  1416. for (uint i=0; i<fPlugins.size(); ++i)
  1417. {
  1418. const PluginInfoCache& info(fPlugins[i]);
  1419. if (search != nullptr && ildaeil::strcasestr(info.name.c_str(), search) == nullptr)
  1420. continue;
  1421. bool selected = fPluginSelected >= 0 && static_cast<uint>(fPluginSelected) == i;
  1422. switch (fPluginType)
  1423. {
  1424. case PLUGIN_INTERNAL:
  1425. case PLUGIN_AU:
  1426. ImGui::TableNextRow();
  1427. ImGui::TableSetColumnIndex(0);
  1428. ImGui::Selectable(info.name.c_str(), &selected);
  1429. ImGui::TableSetColumnIndex(1);
  1430. ImGui::Selectable(info.label.c_str(), &selected);
  1431. break;
  1432. case PLUGIN_LV2:
  1433. ImGui::TableNextRow();
  1434. ImGui::TableSetColumnIndex(0);
  1435. ImGui::Selectable(info.name.c_str(), &selected);
  1436. ImGui::TableSetColumnIndex(1);
  1437. ImGui::Selectable(info.label.c_str(), &selected);
  1438. break;
  1439. default:
  1440. ImGui::TableNextRow();
  1441. ImGui::TableSetColumnIndex(0);
  1442. ImGui::Selectable(info.name.c_str(), &selected);
  1443. ImGui::TableSetColumnIndex(1);
  1444. ImGui::Selectable(info.filename.c_str(), &selected);
  1445. break;
  1446. }
  1447. if (selected)
  1448. fPluginSelected = i;
  1449. }
  1450. ImGui::EndTable();
  1451. }
  1452. ImGui::EndChild();
  1453. }
  1454. }
  1455. ImGui::End();
  1456. }
  1457. protected:
  1458. /* --------------------------------------------------------------------------------------------------------
  1459. * DSP/Plugin Callbacks */
  1460. void parameterChanged(uint32_t, float) override
  1461. {
  1462. }
  1463. void stateChanged(const char* /* const key */, const char*) override
  1464. {
  1465. /*
  1466. if (std::strcmp(key, "project") == 0)
  1467. hidePluginUI(fPlugin->fCarlaHostHandle);
  1468. */
  1469. }
  1470. // -------------------------------------------------------------------------------------------------------
  1471. private:
  1472. /**
  1473. Set our UI class as non-copyable and add a leak detector just in case.
  1474. */
  1475. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(IldaeilUI)
  1476. };
  1477. // --------------------------------------------------------------------------------------------------------------------
  1478. void ildaeilProjectLoadedFromDSP(void* const ui)
  1479. {
  1480. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1481. static_cast<IldaeilUI*>(ui)->projectLoadedFromDSP();
  1482. }
  1483. void ildaeilParameterChangeForUI(void* const ui, const uint32_t index, const float value)
  1484. {
  1485. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1486. static_cast<IldaeilUI*>(ui)->changeParameterFromDSP(index, value);
  1487. }
  1488. void ildaeilCloseUI(void* ui)
  1489. {
  1490. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1491. static_cast<IldaeilUI*>(ui)->closeUI();
  1492. }
  1493. const char* ildaeilOpenFileForUI(void* const ui, const bool isDir, const char* const title, const char* const filter)
  1494. {
  1495. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr, nullptr);
  1496. return static_cast<IldaeilUI*>(ui)->openFileFromDSP(isDir, title, filter);
  1497. }
  1498. /* --------------------------------------------------------------------------------------------------------------------
  1499. * UI entry point, called by DPF to create a new UI instance. */
  1500. UI* createUI()
  1501. {
  1502. return new IldaeilUI();
  1503. }
  1504. // --------------------------------------------------------------------------------------------------------------------
  1505. END_NAMESPACE_DISTRHO