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.

1325 lines
42KB

  1. /*
  2. * DISTRHO Ildaeil Plugin
  3. * Copyright (C) 2021-2022 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. #include "CarlaBackendUtils.hpp"
  20. #include "PluginHostWindow.hpp"
  21. #include "extra/Runner.hpp"
  22. // IDE helper
  23. #include "DearImGui.hpp"
  24. #include <vector>
  25. // strcasestr
  26. #ifdef DISTRHO_OS_WINDOWS
  27. # include <shlwapi.h>
  28. namespace ildaeil {
  29. inline const char* strcasestr(const char* const haystack, const char* const needle)
  30. {
  31. return StrStrIA(haystack, needle);
  32. }
  33. // using strcasestr = StrStrIA;
  34. }
  35. #else
  36. namespace ildaeil {
  37. using ::strcasestr;
  38. }
  39. #endif
  40. // #define WASM_TESTING
  41. START_NAMESPACE_DISTRHO
  42. using namespace CARLA_BACKEND_NAMESPACE;
  43. // --------------------------------------------------------------------------------------------------------------------
  44. class IldaeilUI : public UI,
  45. public Runner,
  46. public PluginHostWindow::Callbacks
  47. {
  48. static constexpr const uint kInitialWidth = 520;
  49. #ifdef DISTRHO_OS_WASM
  50. static constexpr const uint kInitialHeight = 350;
  51. #else
  52. static constexpr const uint kInitialHeight = 520;
  53. #endif
  54. static constexpr const uint kGenericWidth = 380;
  55. static constexpr const uint kGenericHeight = 400;
  56. static constexpr const uint kButtonHeight = 20;
  57. struct PluginInfoCache {
  58. char* name;
  59. char* label;
  60. PluginInfoCache()
  61. : name(nullptr),
  62. label(nullptr) {}
  63. ~PluginInfoCache()
  64. {
  65. std::free(name);
  66. std::free(label);
  67. }
  68. };
  69. struct PluginGenericUI {
  70. char* title;
  71. uint parameterCount;
  72. struct Parameter {
  73. char* name;
  74. char* printformat;
  75. uint32_t rindex;
  76. bool boolean, bvalue, log, readonly;
  77. float min, max, power;
  78. Parameter()
  79. : name(nullptr),
  80. printformat(nullptr),
  81. rindex(0),
  82. boolean(false),
  83. bvalue(false),
  84. log(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. PluginGenericUI()
  95. : title(nullptr),
  96. parameterCount(0),
  97. parameters(nullptr),
  98. values(nullptr) {}
  99. ~PluginGenericUI()
  100. {
  101. std::free(title);
  102. delete[] parameters;
  103. delete[] values;
  104. }
  105. };
  106. enum {
  107. kDrawingLoading,
  108. kDrawingPluginError,
  109. kDrawingPluginList,
  110. kDrawingPluginEmbedUI,
  111. kDrawingPluginGenericUI,
  112. kDrawingErrorInit,
  113. kDrawingErrorDraw
  114. } fDrawingState;
  115. enum {
  116. kIdleInit,
  117. kIdleInitPluginAlreadyLoaded,
  118. kIdleLoadSelectedPlugin,
  119. kIdlePluginLoadedFromDSP,
  120. kIdleResetPlugin,
  121. kIdleOpenFileUI,
  122. kIdleShowCustomUI,
  123. kIdleHideEmbedAndShowGenericUI,
  124. kIdleHidePluginUI,
  125. kIdleGiveIdleToUI,
  126. kIdleChangePluginType,
  127. kIdleNothing
  128. } fIdleState = kIdleInit;
  129. IldaeilBasePlugin* const fPlugin;
  130. PluginHostWindow fPluginHostWindow;
  131. PluginType fPluginType;
  132. PluginType fNextPluginType;
  133. uint fPluginCount;
  134. uint fPluginId;
  135. int fPluginSelected;
  136. bool fPluginScanningFinished;
  137. bool fPluginHasCustomUI;
  138. bool fPluginHasEmbedUI;
  139. bool fPluginHasFileOpen;
  140. bool fPluginHasOutputParameters;
  141. bool fPluginRunning;
  142. bool fPluginWillRunInBridgeMode;
  143. PluginInfoCache* fPlugins;
  144. ScopedPointer<PluginGenericUI> fPluginGenericUI;
  145. bool fPluginSearchActive;
  146. bool fPluginSearchFirstShow;
  147. char fPluginSearchString[0xff];
  148. String fPopupError;
  149. Size<uint> fNextSize;
  150. struct RunnerData {
  151. bool needsReinit;
  152. uint pluginCount;
  153. uint pluginIndex;
  154. RunnerData()
  155. : needsReinit(true),
  156. pluginCount(0),
  157. pluginIndex(0) {}
  158. void init()
  159. {
  160. needsReinit = true;
  161. pluginCount = 0;
  162. pluginIndex = 0;
  163. }
  164. } fRunnerData;
  165. public:
  166. IldaeilUI()
  167. : UI(kInitialWidth, kInitialHeight),
  168. Runner("IldaeilScanner"),
  169. fDrawingState(kDrawingLoading),
  170. fIdleState(kIdleInit),
  171. fPlugin((IldaeilBasePlugin*)getPluginInstancePointer()),
  172. fPluginHostWindow(getWindow(), this),
  173. fPluginType(PLUGIN_LV2),
  174. fNextPluginType(fPluginType),
  175. fPluginCount(0),
  176. fPluginId(0),
  177. fPluginSelected(-1),
  178. fPluginScanningFinished(false),
  179. fPluginHasCustomUI(false),
  180. fPluginHasEmbedUI(false),
  181. fPluginHasFileOpen(false),
  182. fPluginHasOutputParameters(false),
  183. fPluginRunning(false),
  184. fPluginWillRunInBridgeMode(false),
  185. fPlugins(nullptr),
  186. fPluginSearchActive(false),
  187. fPluginSearchFirstShow(false),
  188. fRunnerData()
  189. {
  190. const double scaleFactor = getScaleFactor();
  191. if (fPlugin == nullptr || fPlugin->fCarlaHostHandle == nullptr)
  192. {
  193. fDrawingState = kDrawingErrorInit;
  194. fIdleState = kIdleNothing;
  195. fPopupError = "Ildaeil backend failed to init properly, cannot continue.";
  196. setSize(kInitialWidth * scaleFactor * 0.5, kInitialHeight * scaleFactor * 0.5);
  197. return;
  198. }
  199. std::strcpy(fPluginSearchString, "Search...");
  200. ImGuiStyle& style(ImGui::GetStyle());
  201. style.FrameRounding = 4;
  202. const double padding = style.WindowPadding.y * 2;
  203. if (d_isNotEqual(scaleFactor, 1.0))
  204. {
  205. setSize(kInitialWidth * scaleFactor, kInitialHeight * scaleFactor);
  206. fPluginHostWindow.setPositionAndSize(0, kButtonHeight * scaleFactor + padding,
  207. kInitialWidth * scaleFactor,
  208. (kInitialHeight - kButtonHeight) * scaleFactor - padding);
  209. }
  210. else
  211. {
  212. fPluginHostWindow.setPositionAndSize(0, kButtonHeight + padding,
  213. kInitialWidth, kInitialHeight - kButtonHeight - padding);
  214. }
  215. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  216. char winIdStr[24];
  217. std::snprintf(winIdStr, sizeof(winIdStr), "%lx", (ulong)getWindow().getNativeWindowHandle());
  218. carla_set_engine_option(handle, ENGINE_OPTION_FRONTEND_WIN_ID, 0, winIdStr);
  219. carla_set_engine_option(handle, ENGINE_OPTION_FRONTEND_UI_SCALE, scaleFactor*1000, nullptr);
  220. if (checkIfPluginIsLoaded())
  221. fIdleState = kIdleInitPluginAlreadyLoaded;
  222. fPlugin->fUI = this;
  223. #ifdef WASM_TESTING
  224. if (carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr,
  225. "midifile", 0, 0x0, PLUGIN_OPTIONS_NULL))
  226. {
  227. d_stdout("Special hack for MIDI file playback activated");
  228. carla_set_custom_data(handle, 0, CUSTOM_DATA_TYPE_PATH, "file", "/furelise.mid");
  229. carla_set_parameter_value(handle, 0, 0, 1.0f);
  230. carla_set_parameter_value(handle, 0, 1, 0.0f);
  231. fPluginId = 2;
  232. }
  233. carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr, "miditranspose", 0, 0x0, PLUGIN_OPTIONS_NULL);
  234. carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr, "bypass", 0, 0x0, PLUGIN_OPTIONS_NULL);
  235. carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr, "3bandeq", 0, 0x0, PLUGIN_OPTIONS_NULL);
  236. carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr, "pingpongpan", 0, 0x0, PLUGIN_OPTIONS_NULL);
  237. carla_set_parameter_value(handle, 4, 1, 0.0f);
  238. carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr, "audiogain_s", 0, 0x0, PLUGIN_OPTIONS_NULL);
  239. for (uint i=0; i<5; ++i)
  240. carla_add_plugin(handle, BINARY_NATIVE, PLUGIN_INTERNAL, nullptr, nullptr, "bypass", 0, 0x0, PLUGIN_OPTIONS_NULL);
  241. #endif
  242. }
  243. ~IldaeilUI() override
  244. {
  245. if (fPlugin != nullptr && fPlugin->fCarlaHostHandle != nullptr)
  246. {
  247. fPlugin->fUI = nullptr;
  248. if (fPluginRunning)
  249. hidePluginUI(fPlugin->fCarlaHostHandle);
  250. carla_set_engine_option(fPlugin->fCarlaHostHandle, ENGINE_OPTION_FRONTEND_WIN_ID, 0, "0");
  251. }
  252. stopRunner();
  253. fPluginGenericUI = nullptr;
  254. delete[] fPlugins;
  255. }
  256. bool checkIfPluginIsLoaded()
  257. {
  258. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  259. if (carla_get_current_plugin_count(handle) == 0)
  260. return false;
  261. const uint hints = carla_get_plugin_info(handle, fPluginId)->hints;
  262. updatePluginFlags(hints);
  263. fPluginRunning = true;
  264. return true;
  265. }
  266. void updatePluginFlags(const uint hints) noexcept
  267. {
  268. if (hints & PLUGIN_HAS_CUSTOM_UI_USING_FILE_OPEN)
  269. {
  270. fPluginHasCustomUI = false;
  271. fPluginHasEmbedUI = false;
  272. fPluginHasFileOpen = true;
  273. }
  274. else
  275. {
  276. fPluginHasCustomUI = hints & PLUGIN_HAS_CUSTOM_UI;
  277. #ifndef DISTRHO_OS_WASM
  278. fPluginHasEmbedUI = hints & PLUGIN_HAS_CUSTOM_EMBED_UI;
  279. #endif
  280. fPluginHasFileOpen = false;
  281. }
  282. }
  283. void projectLoadedFromDSP()
  284. {
  285. if (checkIfPluginIsLoaded())
  286. fIdleState = kIdlePluginLoadedFromDSP;
  287. }
  288. void changeParameterFromDSP(const uint32_t index, const float value)
  289. {
  290. if (PluginGenericUI* const ui = fPluginGenericUI)
  291. {
  292. for (uint32_t i=0; i < ui->parameterCount; ++i)
  293. {
  294. if (ui->parameters[i].rindex != index)
  295. continue;
  296. ui->values[i] = value;
  297. if (ui->parameters[i].boolean)
  298. ui->parameters[i].bvalue = value > ui->parameters[i].min;
  299. break;
  300. }
  301. }
  302. repaint();
  303. }
  304. void closeUI()
  305. {
  306. if (fIdleState == kIdleGiveIdleToUI)
  307. fIdleState = kIdleNothing;
  308. }
  309. const char* openFileFromDSP(const bool /*isDir*/, const char* const title, const char* const /*filter*/)
  310. {
  311. DISTRHO_SAFE_ASSERT_RETURN(fPluginType == PLUGIN_INTERNAL || fPluginType == PLUGIN_LV2, nullptr);
  312. FileBrowserOptions opts;
  313. opts.title = title;
  314. openFileBrowser(opts);
  315. return nullptr;
  316. }
  317. void showPluginUI(const CarlaHostHandle handle, const bool showIfNotEmbed)
  318. {
  319. const uint hints = carla_get_plugin_info(handle, fPluginId)->hints;
  320. #ifndef DISTRHO_OS_WASM
  321. if (hints & PLUGIN_HAS_CUSTOM_EMBED_UI)
  322. {
  323. fDrawingState = kDrawingPluginEmbedUI;
  324. fIdleState = kIdleGiveIdleToUI;
  325. fPluginHasCustomUI = true;
  326. fPluginHasEmbedUI = true;
  327. fPluginHasFileOpen = false;
  328. carla_embed_custom_ui(handle, fPluginId, fPluginHostWindow.attachAndGetWindowHandle());
  329. }
  330. else
  331. #endif
  332. {
  333. // fPluginHas* flags are updated in the next function
  334. createOrUpdatePluginGenericUI(handle);
  335. if (showIfNotEmbed && fPluginHasCustomUI)
  336. {
  337. fIdleState = kIdleGiveIdleToUI;
  338. carla_show_custom_ui(handle, fPluginId, true);
  339. }
  340. }
  341. repaint();
  342. }
  343. void hidePluginUI(const CarlaHostHandle handle)
  344. {
  345. DISTRHO_SAFE_ASSERT_RETURN(fPluginRunning,);
  346. fPluginHostWindow.hide();
  347. carla_show_custom_ui(handle, fPluginId, false);
  348. }
  349. void createOrUpdatePluginGenericUI(const CarlaHostHandle handle, const CarlaPluginInfo* info = nullptr)
  350. {
  351. if (info == nullptr)
  352. info = carla_get_plugin_info(handle, fPluginId);
  353. fDrawingState = kDrawingPluginGenericUI;
  354. updatePluginFlags(info->hints);
  355. if (fPluginGenericUI == nullptr)
  356. createPluginGenericUI(handle, info);
  357. else
  358. updatePluginGenericUI(handle);
  359. #ifndef DISTRHO_OS_WASM
  360. ImGuiStyle& style(ImGui::GetStyle());
  361. const double scaleFactor = getScaleFactor();
  362. fNextSize = Size<uint>(kGenericWidth * scaleFactor, (kGenericHeight + style.FramePadding.x) * scaleFactor);
  363. #endif
  364. }
  365. void createPluginGenericUI(const CarlaHostHandle handle, const CarlaPluginInfo* const info)
  366. {
  367. PluginGenericUI* const ui = new PluginGenericUI;
  368. String title(info->name);
  369. title += " by ";
  370. title += info->maker;
  371. ui->title = title.getAndReleaseBuffer();
  372. const uint32_t pcount = ui->parameterCount = carla_get_parameter_count(handle, fPluginId);
  373. // make count of valid parameters
  374. for (uint32_t i=0; i < pcount; ++i)
  375. {
  376. const ParameterData* const pdata = carla_get_parameter_data(handle, fPluginId, i);
  377. if ((pdata->hints & PARAMETER_IS_ENABLED) == 0x0)
  378. {
  379. --ui->parameterCount;
  380. continue;
  381. }
  382. if (pdata->type == PARAMETER_OUTPUT)
  383. fPluginHasOutputParameters = true;
  384. }
  385. ui->parameters = new PluginGenericUI::Parameter[ui->parameterCount];
  386. ui->values = new float[ui->parameterCount];
  387. // now safely fill in details
  388. for (uint32_t i=0, j=0; i < pcount; ++i)
  389. {
  390. const ParameterData* const pdata = carla_get_parameter_data(handle, fPluginId, i);
  391. if ((pdata->hints & PARAMETER_IS_ENABLED) == 0x0)
  392. continue;
  393. const CarlaParameterInfo* const pinfo = carla_get_parameter_info(handle, fPluginId, i);
  394. const ::ParameterRanges* const pranges = carla_get_parameter_ranges(handle, fPluginId, i);
  395. String printformat;
  396. if (pdata->hints & PARAMETER_IS_INTEGER)
  397. printformat = "%.0f ";
  398. else
  399. printformat = "%.3f ";
  400. printformat += pinfo->unit;
  401. PluginGenericUI::Parameter& param(ui->parameters[j]);
  402. param.name = strdup(pinfo->name);
  403. param.printformat = printformat.getAndReleaseBuffer();
  404. param.rindex = i;
  405. param.boolean = pdata->hints & PARAMETER_IS_BOOLEAN;
  406. param.log = pdata->hints & PARAMETER_IS_LOGARITHMIC;
  407. param.readonly = pdata->type != PARAMETER_INPUT || (pdata->hints & PARAMETER_IS_READ_ONLY);
  408. param.min = pranges->min;
  409. param.max = pranges->max;
  410. ui->values[j] = carla_get_current_parameter_value(handle, fPluginId, i);
  411. if (param.boolean)
  412. param.bvalue = ui->values[j] > param.min;
  413. else
  414. param.bvalue = false;
  415. ++j;
  416. }
  417. fPluginGenericUI = ui;
  418. }
  419. void updatePluginGenericUI(const CarlaHostHandle handle)
  420. {
  421. PluginGenericUI* const ui = fPluginGenericUI;
  422. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  423. for (uint32_t i=0; i < ui->parameterCount; ++i)
  424. {
  425. ui->values[i] = carla_get_current_parameter_value(handle, fPluginId, ui->parameters[i].rindex);
  426. if (ui->parameters[i].boolean)
  427. ui->parameters[i].bvalue = ui->values[i] > ui->parameters[i].min;
  428. }
  429. }
  430. void loadPlugin(const CarlaHostHandle handle, const char* const label)
  431. {
  432. if (fPluginRunning || fPluginId != 0)
  433. {
  434. hidePluginUI(handle);
  435. carla_replace_plugin(handle, fPluginId);
  436. }
  437. carla_set_engine_option(handle, ENGINE_OPTION_PREFER_PLUGIN_BRIDGES, fPluginWillRunInBridgeMode, nullptr);
  438. const MutexLocker cml(fPlugin->sPluginInfoLoadMutex);
  439. if (carla_add_plugin(handle, BINARY_NATIVE, fPluginType, nullptr, nullptr,
  440. label, 0, 0x0, PLUGIN_OPTIONS_NULL))
  441. {
  442. fPluginRunning = true;
  443. fPluginGenericUI = nullptr;
  444. showPluginUI(handle, false);
  445. #ifdef WASM_TESTING
  446. d_stdout("loaded a plugin with label '%s'", label);
  447. if (std::strcmp(label, "audiofile") == 0)
  448. {
  449. d_stdout("Loading mp3 file into audiofile plugin");
  450. carla_set_custom_data(handle, fPluginId, CUSTOM_DATA_TYPE_PATH, "file", "/foolme.mp3");
  451. carla_set_parameter_value(handle, fPluginId, 1, 0.0f);
  452. fPluginGenericUI->values[1] = 0.0f;
  453. }
  454. #endif
  455. }
  456. else
  457. {
  458. fPopupError = carla_get_last_error(handle);
  459. d_stdout("got error: %s", fPopupError.buffer());
  460. fDrawingState = kDrawingPluginError;
  461. }
  462. repaint();
  463. }
  464. protected:
  465. void pluginWindowResized(const uint width, const uint height) override
  466. {
  467. const uint extraHeight = kButtonHeight * getScaleFactor() + ImGui::GetStyle().WindowPadding.y * 2;
  468. fNextSize = Size<uint>(width, height + extraHeight);
  469. }
  470. void uiIdle() override
  471. {
  472. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  473. DISTRHO_SAFE_ASSERT_RETURN(handle != nullptr,);
  474. // carla_juce_idle();
  475. if (fDrawingState == kDrawingPluginGenericUI && fPluginGenericUI != nullptr && fPluginHasOutputParameters)
  476. {
  477. updatePluginGenericUI(handle);
  478. repaint();
  479. }
  480. if (fNextSize.isValid())
  481. {
  482. setSize(fNextSize);
  483. fNextSize = Size<uint>();
  484. }
  485. switch (fIdleState)
  486. {
  487. case kIdleInit:
  488. fIdleState = kIdleNothing;
  489. initAndStartRunner();
  490. break;
  491. case kIdleInitPluginAlreadyLoaded:
  492. fIdleState = kIdleNothing;
  493. showPluginUI(handle, false);
  494. initAndStartRunner();
  495. break;
  496. case kIdlePluginLoadedFromDSP:
  497. fIdleState = kIdleNothing;
  498. showPluginUI(handle, false);
  499. break;
  500. case kIdleLoadSelectedPlugin:
  501. fIdleState = kIdleNothing;
  502. loadSelectedPlugin(handle);
  503. break;
  504. case kIdleResetPlugin:
  505. fIdleState = kIdleNothing;
  506. loadPlugin(handle, carla_get_plugin_info(handle, fPluginId)->label);
  507. break;
  508. case kIdleOpenFileUI:
  509. fIdleState = kIdleNothing;
  510. carla_show_custom_ui(handle, fPluginId, true);
  511. break;
  512. case kIdleShowCustomUI:
  513. fIdleState = kIdleNothing;
  514. showPluginUI(handle, true);
  515. break;
  516. case kIdleHideEmbedAndShowGenericUI:
  517. fIdleState = kIdleNothing;
  518. hidePluginUI(handle);
  519. createOrUpdatePluginGenericUI(handle);
  520. break;
  521. case kIdleHidePluginUI:
  522. fIdleState = kIdleNothing;
  523. carla_show_custom_ui(handle, fPluginId, false);
  524. break;
  525. case kIdleGiveIdleToUI:
  526. if (fPlugin->fCarlaPluginDescriptor->ui_idle != nullptr)
  527. fPlugin->fCarlaPluginDescriptor->ui_idle(fPlugin->fCarlaPluginHandle);
  528. fPluginHostWindow.idle();
  529. break;
  530. case kIdleChangePluginType:
  531. fIdleState = kIdleNothing;
  532. if (fPluginRunning)
  533. hidePluginUI(handle);
  534. fPluginSelected = -1;
  535. stopRunner();
  536. fPluginType = fNextPluginType;
  537. initAndStartRunner();
  538. break;
  539. case kIdleNothing:
  540. break;
  541. }
  542. }
  543. void loadSelectedPlugin(const CarlaHostHandle handle)
  544. {
  545. DISTRHO_SAFE_ASSERT_RETURN(fPluginSelected >= 0,);
  546. const PluginInfoCache& info(fPlugins[fPluginSelected]);
  547. const char* label = nullptr;
  548. switch (fPluginType)
  549. {
  550. case PLUGIN_INTERNAL:
  551. case PLUGIN_AU:
  552. case PLUGIN_JSFX:
  553. case PLUGIN_SFZ:
  554. label = info.label;
  555. break;
  556. case PLUGIN_LV2: {
  557. const char* const slash = std::strchr(info.label, DISTRHO_OS_SEP);
  558. DISTRHO_SAFE_ASSERT_RETURN(slash != nullptr,);
  559. label = slash+1;
  560. break;
  561. }
  562. default:
  563. break;
  564. }
  565. DISTRHO_SAFE_ASSERT_RETURN(label != nullptr,);
  566. d_stdout("Loading %s...", info.name);
  567. loadPlugin(handle, label);
  568. }
  569. void uiFileBrowserSelected(const char* const filename) override
  570. {
  571. if (fPlugin != nullptr && fPlugin->fCarlaHostHandle != nullptr && filename != nullptr)
  572. carla_set_custom_data(fPlugin->fCarlaHostHandle, fPluginId, CUSTOM_DATA_TYPE_STRING, "file", filename);
  573. }
  574. bool initAndStartRunner()
  575. {
  576. if (isRunnerActive())
  577. stopRunner();
  578. fRunnerData.init();
  579. return startRunner();
  580. }
  581. bool run() override
  582. {
  583. if (fRunnerData.needsReinit)
  584. {
  585. fRunnerData.needsReinit = false;
  586. const char* path;
  587. switch (fPluginType)
  588. {
  589. case PLUGIN_LV2:
  590. path = std::getenv("LV2_PATH");
  591. break;
  592. case PLUGIN_JSFX:
  593. path = fPlugin->getPathForJSFX();
  594. break;
  595. default:
  596. path = nullptr;
  597. break;
  598. }
  599. fPluginCount = 0;
  600. delete[] fPlugins;
  601. {
  602. const MutexLocker cml(fPlugin->sPluginInfoLoadMutex);
  603. d_stdout("Will scan plugins now...");
  604. fRunnerData.pluginCount = carla_get_cached_plugin_count(fPluginType, path);
  605. d_stdout("Scanning found %u plugins", fRunnerData.pluginCount);
  606. }
  607. if (fDrawingState == kDrawingLoading)
  608. {
  609. fDrawingState = kDrawingPluginList;
  610. fPluginSearchFirstShow = true;
  611. }
  612. if (fRunnerData.pluginCount != 0)
  613. {
  614. fPlugins = new PluginInfoCache[fRunnerData.pluginCount];
  615. fPluginScanningFinished = false;
  616. return true;
  617. }
  618. else
  619. {
  620. fPlugins = nullptr;
  621. fPluginScanningFinished = true;
  622. return false;
  623. }
  624. }
  625. const uint index = fRunnerData.pluginIndex++;
  626. DISTRHO_SAFE_ASSERT_UINT2_RETURN(index < fRunnerData.pluginCount,
  627. index, fRunnerData.pluginCount, false);
  628. do {
  629. const MutexLocker cml(fPlugin->sPluginInfoLoadMutex);
  630. const CarlaCachedPluginInfo* const info = carla_get_cached_plugin_info(fPluginType, index);
  631. DISTRHO_SAFE_ASSERT_RETURN(info != nullptr, true);
  632. if (! info->valid)
  633. break;
  634. if (info->cvIns != 0 || info->cvOuts != 0)
  635. break;
  636. #ifdef DISTRHO_OS_WASM
  637. if (info->midiIns != 0 && info->midiIns != 1)
  638. break;
  639. if (info->midiOuts != 0 && info->midiOuts != 1)
  640. break;
  641. if (info->audioIns > 2 || info->audioOuts > 2)
  642. break;
  643. if (fPluginType == PLUGIN_INTERNAL)
  644. {
  645. if (std::strcmp(info->label, "audiogain") == 0)
  646. break;
  647. if (std::strcmp(info->label, "midichanfilter") == 0)
  648. break;
  649. if (std::strcmp(info->label, "midichannelize") == 0)
  650. break;
  651. }
  652. #elif DISTRHO_PLUGIN_IS_SYNTH
  653. if (info->midiIns != 1 && info->audioIns != 0)
  654. break;
  655. if ((info->hints & PLUGIN_IS_SYNTH) == 0x0 && info->audioIns != 0)
  656. break;
  657. if (info->audioOuts != 1 && info->audioOuts != 2)
  658. break;
  659. #elif DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  660. if ((info->midiIns != 1 && info->audioIns != 0 && info->audioOuts != 0) || info->midiOuts != 1)
  661. break;
  662. if (info->audioIns != 0 || info->audioOuts != 0)
  663. break;
  664. #else
  665. if (info->audioIns != 1 && info->audioIns != 2)
  666. break;
  667. if (info->audioOuts != 1 && info->audioOuts != 2)
  668. break;
  669. #endif
  670. if (fPluginType == PLUGIN_INTERNAL)
  671. {
  672. #ifndef DISTRHO_OS_WASM
  673. if (std::strcmp(info->label, "audiogain_s") == 0)
  674. break;
  675. #endif
  676. if (std::strcmp(info->label, "lfo") == 0)
  677. break;
  678. if (std::strcmp(info->label, "midi2cv") == 0)
  679. break;
  680. if (std::strcmp(info->label, "midithrough") == 0)
  681. break;
  682. if (std::strcmp(info->label, "3bandsplitter") == 0)
  683. break;
  684. }
  685. const uint pindex = fPluginCount;
  686. fPlugins[pindex].name = strdup(info->name);
  687. fPlugins[pindex].label = strdup(info->label);
  688. ++fPluginCount;
  689. } while (false);
  690. // run again
  691. if (fRunnerData.pluginIndex != fRunnerData.pluginCount)
  692. return true;
  693. // stop here
  694. fPluginScanningFinished = true;
  695. return false;
  696. }
  697. void onImGuiDisplay() override
  698. {
  699. switch (fDrawingState)
  700. {
  701. case kDrawingLoading:
  702. drawLoading();
  703. break;
  704. case kDrawingPluginError:
  705. ImGui::OpenPopup("Plugin Error");
  706. // call ourselves again with the plugin list
  707. fDrawingState = kDrawingPluginList;
  708. onImGuiDisplay();
  709. break;
  710. case kDrawingPluginList:
  711. drawPluginList();
  712. break;
  713. case kDrawingPluginGenericUI:
  714. drawTopBar();
  715. drawGenericUI();
  716. break;
  717. case kDrawingPluginEmbedUI:
  718. drawTopBar();
  719. break;
  720. case kDrawingErrorInit:
  721. fDrawingState = kDrawingErrorDraw;
  722. drawError(true);
  723. break;
  724. case kDrawingErrorDraw:
  725. drawError(false);
  726. break;
  727. }
  728. }
  729. void drawError(const bool open)
  730. {
  731. ImGui::SetNextWindowPos(ImVec2(0, 0));
  732. ImGui::SetNextWindowSize(ImVec2(getWidth(), getHeight()));
  733. const int flags = ImGuiWindowFlags_NoSavedSettings
  734. | ImGuiWindowFlags_NoTitleBar
  735. | ImGuiWindowFlags_NoResize
  736. | ImGuiWindowFlags_NoCollapse
  737. | ImGuiWindowFlags_NoScrollbar
  738. | ImGuiWindowFlags_NoScrollWithMouse;
  739. if (ImGui::Begin("Error Window", nullptr, flags))
  740. {
  741. if (open)
  742. ImGui::OpenPopup("Engine Error");
  743. const int pflags = ImGuiWindowFlags_NoSavedSettings
  744. | ImGuiWindowFlags_NoResize
  745. | ImGuiWindowFlags_NoCollapse
  746. | ImGuiWindowFlags_NoScrollbar
  747. | ImGuiWindowFlags_NoScrollWithMouse
  748. | ImGuiWindowFlags_AlwaysAutoResize
  749. | ImGuiWindowFlags_AlwaysUseWindowPadding;
  750. if (ImGui::BeginPopupModal("Engine Error", nullptr, pflags))
  751. {
  752. ImGui::TextUnformatted(fPopupError.buffer(), nullptr);
  753. ImGui::EndPopup();
  754. }
  755. }
  756. ImGui::End();
  757. }
  758. void drawTopBar()
  759. {
  760. const float padding = ImGui::GetStyle().WindowPadding.y * 2;
  761. ImGui::SetNextWindowPos(ImVec2(0, 0));
  762. ImGui::SetNextWindowSize(ImVec2(getWidth(), kButtonHeight * getScaleFactor() + padding));
  763. const int flags = ImGuiWindowFlags_NoSavedSettings
  764. | ImGuiWindowFlags_NoTitleBar
  765. | ImGuiWindowFlags_NoResize
  766. | ImGuiWindowFlags_NoCollapse
  767. | ImGuiWindowFlags_NoScrollbar
  768. | ImGuiWindowFlags_NoScrollWithMouse;
  769. if (ImGui::Begin("Current Plugin", nullptr, flags))
  770. {
  771. if (ImGui::Button("Pick Another..."))
  772. {
  773. fIdleState = kIdleHidePluginUI;
  774. fDrawingState = kDrawingPluginList;
  775. #ifndef DISTRHO_OS_WASM
  776. const double scaleFactor = getScaleFactor();
  777. fNextSize = Size<uint>(kInitialWidth * scaleFactor, kInitialHeight * scaleFactor);
  778. #endif
  779. }
  780. ImGui::SameLine();
  781. if (ImGui::Button("Reset"))
  782. fIdleState = kIdleResetPlugin;
  783. if (fDrawingState == kDrawingPluginGenericUI)
  784. {
  785. if (fPluginHasCustomUI)
  786. {
  787. ImGui::SameLine();
  788. if (ImGui::Button("Show Custom GUI"))
  789. fIdleState = kIdleShowCustomUI;
  790. }
  791. if (fPluginHasFileOpen)
  792. {
  793. ImGui::SameLine();
  794. if (ImGui::Button("Open File..."))
  795. fIdleState = kIdleOpenFileUI;
  796. }
  797. #ifdef WASM_TESTING
  798. ImGui::SameLine();
  799. ImGui::TextUnformatted(" Plugin to control:");
  800. for (uint i=1; i<10; ++i)
  801. {
  802. char txt[8];
  803. sprintf(txt, "%d", i);
  804. ImGui::SameLine();
  805. if (ImGui::Button(txt))
  806. {
  807. fPluginId = i;
  808. fPluginGenericUI = nullptr;
  809. fIdleState = kIdleHideEmbedAndShowGenericUI;
  810. }
  811. }
  812. #endif
  813. }
  814. if (fDrawingState == kDrawingPluginEmbedUI)
  815. {
  816. ImGui::SameLine();
  817. if (ImGui::Button("Show Generic GUI"))
  818. fIdleState = kIdleHideEmbedAndShowGenericUI;
  819. }
  820. }
  821. ImGui::End();
  822. }
  823. void setupMainWindowPos()
  824. {
  825. const float scaleFactor = getScaleFactor();
  826. float y = 0;
  827. float height = getHeight();
  828. if (fDrawingState == kDrawingPluginGenericUI)
  829. {
  830. y = kButtonHeight * scaleFactor + ImGui::GetStyle().WindowPadding.y * 2 - scaleFactor;
  831. height -= y;
  832. }
  833. ImGui::SetNextWindowPos(ImVec2(0, y));
  834. ImGui::SetNextWindowSize(ImVec2(getWidth(), height));
  835. }
  836. void drawGenericUI()
  837. {
  838. setupMainWindowPos();
  839. PluginGenericUI* const ui = fPluginGenericUI;
  840. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  841. const int pflags = ImGuiWindowFlags_NoSavedSettings
  842. | ImGuiWindowFlags_NoResize
  843. | ImGuiWindowFlags_NoCollapse
  844. | ImGuiWindowFlags_AlwaysAutoResize;
  845. if (ImGui::Begin(ui->title, nullptr, pflags))
  846. {
  847. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  848. for (uint32_t i=0; i < ui->parameterCount; ++i)
  849. {
  850. PluginGenericUI::Parameter& param(ui->parameters[i]);
  851. if (param.readonly)
  852. {
  853. ImGui::BeginDisabled();
  854. ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.printformat, ImGuiSliderFlags_NoInput);
  855. ImGui::EndDisabled();
  856. continue;
  857. }
  858. if (param.boolean)
  859. {
  860. if (ImGui::Checkbox(param.name, &ui->parameters[i].bvalue))
  861. {
  862. if (ImGui::IsItemActivated())
  863. {
  864. carla_set_parameter_touch(handle, fPluginId, param.rindex, true);
  865. // editParameter(0, true);
  866. }
  867. ui->values[i] = ui->parameters[i].bvalue ? ui->parameters[i].max : ui->parameters[i].min;
  868. carla_set_parameter_value(handle, fPluginId, param.rindex, ui->values[i]);
  869. // setParameterValue(0, ui->values[i]);
  870. }
  871. }
  872. else
  873. {
  874. const bool ret = param.log
  875. ? ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.printformat, 2.0f)
  876. : ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.printformat);
  877. if (ret)
  878. {
  879. if (ImGui::IsItemActivated())
  880. {
  881. carla_set_parameter_touch(handle, fPluginId, param.rindex, true);
  882. // editParameter(0, true);
  883. }
  884. carla_set_parameter_value(handle, fPluginId, param.rindex, ui->values[i]);
  885. // setParameterValue(0, ui->values[i]);
  886. }
  887. }
  888. if (ImGui::IsItemDeactivated())
  889. {
  890. carla_set_parameter_touch(handle, fPluginId, param.rindex, false);
  891. // editParameter(0, false);
  892. }
  893. }
  894. }
  895. ImGui::End();
  896. }
  897. void drawLoading()
  898. {
  899. setupMainWindowPos();
  900. if (ImGui::Begin("Plugin List", nullptr, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize))
  901. ImGui::TextUnformatted("Loading...", nullptr);
  902. ImGui::End();
  903. }
  904. void drawPluginList()
  905. {
  906. static const char* pluginTypes[] = {
  907. getPluginTypeAsString(PLUGIN_INTERNAL),
  908. getPluginTypeAsString(PLUGIN_LV2),
  909. getPluginTypeAsString(PLUGIN_JSFX),
  910. };
  911. setupMainWindowPos();
  912. if (ImGui::Begin("Plugin List", nullptr, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize))
  913. {
  914. const int pflags = ImGuiWindowFlags_NoSavedSettings
  915. | ImGuiWindowFlags_NoResize
  916. | ImGuiWindowFlags_NoCollapse
  917. | ImGuiWindowFlags_NoScrollbar
  918. | ImGuiWindowFlags_NoScrollWithMouse
  919. | ImGuiWindowFlags_AlwaysAutoResize;
  920. if (ImGui::BeginPopupModal("Plugin Error", nullptr, pflags))
  921. {
  922. ImGui::TextWrapped("Failed to load plugin, error was:\n%s", fPopupError.buffer());
  923. ImGui::Separator();
  924. if (ImGui::Button("Ok"))
  925. ImGui::CloseCurrentPopup();
  926. ImGui::SameLine();
  927. ImGui::Dummy(ImVec2(500 * getScaleFactor(), 1));
  928. ImGui::EndPopup();
  929. }
  930. else if (fPluginSearchFirstShow)
  931. {
  932. fPluginSearchFirstShow = false;
  933. ImGui::SetKeyboardFocusHere();
  934. }
  935. if (ImGui::InputText("##pluginsearch", fPluginSearchString, sizeof(fPluginSearchString)-1,
  936. ImGuiInputTextFlags_CharsNoBlank|ImGuiInputTextFlags_AutoSelectAll))
  937. fPluginSearchActive = true;
  938. if (ImGui::IsKeyDown(ImGuiKey_Escape))
  939. fPluginSearchActive = false;
  940. ImGui::SameLine();
  941. ImGui::PushItemWidth(-1.0f);
  942. int current;
  943. switch (fPluginType)
  944. {
  945. case PLUGIN_JSFX:
  946. current = 2;
  947. break;
  948. case PLUGIN_LV2:
  949. current = 1;
  950. break;
  951. default:
  952. current = 0;
  953. break;
  954. }
  955. if (ImGui::Combo("##plugintypes", &current, pluginTypes, ARRAY_SIZE(pluginTypes)))
  956. {
  957. fIdleState = kIdleChangePluginType;
  958. switch (current)
  959. {
  960. case 0:
  961. fNextPluginType = PLUGIN_INTERNAL;
  962. break;
  963. case 1:
  964. fNextPluginType = PLUGIN_LV2;
  965. break;
  966. case 2:
  967. fNextPluginType = PLUGIN_JSFX;
  968. break;
  969. }
  970. }
  971. ImGui::BeginDisabled(!fPluginScanningFinished || fPluginSelected < 0);
  972. if (ImGui::Button("Load Plugin"))
  973. fIdleState = kIdleLoadSelectedPlugin;
  974. // xx cardinal
  975. if (fPluginType != PLUGIN_INTERNAL /*&& module->canUseBridges*/)
  976. {
  977. ImGui::SameLine();
  978. ImGui::Checkbox("Run in bridge mode", &fPluginWillRunInBridgeMode);
  979. }
  980. ImGui::EndDisabled();
  981. if (fPluginRunning)
  982. {
  983. ImGui::SameLine();
  984. if (ImGui::Button("Cancel"))
  985. fIdleState = kIdleShowCustomUI;
  986. }
  987. if (ImGui::BeginChild("pluginlistwindow"))
  988. {
  989. if (ImGui::BeginTable("pluginlist", 2, ImGuiTableFlags_NoSavedSettings))
  990. {
  991. const char* const search = fPluginSearchActive && fPluginSearchString[0] != '\0' ? fPluginSearchString : nullptr;
  992. switch (fPluginType)
  993. {
  994. case PLUGIN_INTERNAL:
  995. case PLUGIN_AU:
  996. case PLUGIN_SFZ:
  997. case PLUGIN_JSFX:
  998. ImGui::TableSetupColumn("Name");
  999. ImGui::TableSetupColumn("Label");
  1000. ImGui::TableHeadersRow();
  1001. break;
  1002. case PLUGIN_LV2:
  1003. ImGui::TableSetupColumn("Name");
  1004. ImGui::TableSetupColumn("URI");
  1005. ImGui::TableHeadersRow();
  1006. break;
  1007. default:
  1008. break;
  1009. }
  1010. for (uint i=0; i<fPluginCount; ++i)
  1011. {
  1012. const PluginInfoCache& info(fPlugins[i]);
  1013. if (search != nullptr && ildaeil::strcasestr(info.name, search) == nullptr)
  1014. continue;
  1015. bool selected = fPluginSelected >= 0 && static_cast<uint>(fPluginSelected) == i;
  1016. switch (fPluginType)
  1017. {
  1018. case PLUGIN_INTERNAL:
  1019. case PLUGIN_AU:
  1020. case PLUGIN_JSFX:
  1021. case PLUGIN_SFZ:
  1022. ImGui::TableNextRow();
  1023. ImGui::TableSetColumnIndex(0);
  1024. ImGui::Selectable(info.name, &selected);
  1025. ImGui::TableSetColumnIndex(1);
  1026. ImGui::Selectable(info.label, &selected);
  1027. break;
  1028. case PLUGIN_LV2: {
  1029. const char* const slash = std::strchr(info.label, DISTRHO_OS_SEP);
  1030. DISTRHO_SAFE_ASSERT_CONTINUE(slash != nullptr);
  1031. ImGui::TableNextRow();
  1032. ImGui::TableSetColumnIndex(0);
  1033. ImGui::Selectable(info.name, &selected);
  1034. ImGui::TableSetColumnIndex(1);
  1035. ImGui::Selectable(slash+1, &selected);
  1036. break;
  1037. }
  1038. default:
  1039. break;
  1040. }
  1041. if (selected)
  1042. fPluginSelected = i;
  1043. }
  1044. ImGui::EndTable();
  1045. }
  1046. ImGui::EndChild();
  1047. }
  1048. }
  1049. ImGui::End();
  1050. }
  1051. protected:
  1052. /* --------------------------------------------------------------------------------------------------------
  1053. * DSP/Plugin Callbacks */
  1054. void parameterChanged(uint32_t, float) override
  1055. {
  1056. }
  1057. void stateChanged(const char* /* const key */, const char*) override
  1058. {
  1059. /*
  1060. if (std::strcmp(key, "project") == 0)
  1061. hidePluginUI(fPlugin->fCarlaHostHandle);
  1062. */
  1063. }
  1064. // -------------------------------------------------------------------------------------------------------
  1065. private:
  1066. /**
  1067. Set our UI class as non-copyable and add a leak detector just in case.
  1068. */
  1069. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(IldaeilUI)
  1070. };
  1071. // --------------------------------------------------------------------------------------------------------------------
  1072. void ildaeilProjectLoadedFromDSP(void* const ui)
  1073. {
  1074. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1075. static_cast<IldaeilUI*>(ui)->projectLoadedFromDSP();
  1076. }
  1077. void ildaeilParameterChangeForUI(void* const ui, const uint32_t index, const float value)
  1078. {
  1079. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1080. static_cast<IldaeilUI*>(ui)->changeParameterFromDSP(index, value);
  1081. }
  1082. void ildaeilCloseUI(void* ui)
  1083. {
  1084. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1085. d_stdout("%s %d", __PRETTY_FUNCTION__, __LINE__);
  1086. static_cast<IldaeilUI*>(ui)->closeUI();
  1087. }
  1088. const char* ildaeilOpenFileForUI(void* const ui, const bool isDir, const char* const title, const char* const filter)
  1089. {
  1090. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr, nullptr);
  1091. d_stdout("%s %d", __PRETTY_FUNCTION__, __LINE__);
  1092. return static_cast<IldaeilUI*>(ui)->openFileFromDSP(isDir, title, filter);
  1093. }
  1094. /* --------------------------------------------------------------------------------------------------------------------
  1095. * UI entry point, called by DPF to create a new UI instance. */
  1096. UI* createUI()
  1097. {
  1098. return new IldaeilUI();
  1099. }
  1100. // --------------------------------------------------------------------------------------------------------------------
  1101. END_NAMESPACE_DISTRHO