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.

1740 lines
58KB

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