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.

1835 lines
61KB

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