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.

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