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.

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