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.

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