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.

1277 lines
41KB

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