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.

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