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.

1376 lines
44KB

  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, fPluginFilename;
  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. if (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. fPluginFilename.clear();
  445. showPluginUI(handle, false);
  446. #ifdef WASM_TESTING
  447. d_stdout("loaded a plugin with label '%s'", label);
  448. if (std::strcmp(label, "audiofile") == 0)
  449. {
  450. d_stdout("Loading mp3 file into audiofile plugin");
  451. carla_set_custom_data(handle, fPluginId, CUSTOM_DATA_TYPE_PATH, "file", "/foolme.mp3");
  452. carla_set_parameter_value(handle, fPluginId, 1, 0.0f);
  453. fPluginGenericUI->values[1] = 0.0f;
  454. }
  455. #endif
  456. }
  457. else
  458. {
  459. fPopupError = carla_get_last_error(handle);
  460. d_stdout("got error: %s", fPopupError.buffer());
  461. fDrawingState = kDrawingPluginError;
  462. }
  463. repaint();
  464. }
  465. void loadFileAsPlugin(const CarlaHostHandle handle, const char* const filename)
  466. {
  467. if (fPluginRunning || fPluginId != 0)
  468. {
  469. hidePluginUI(handle);
  470. carla_replace_plugin(handle, fPluginId);
  471. }
  472. carla_set_engine_option(handle, ENGINE_OPTION_PREFER_PLUGIN_BRIDGES, fPluginWillRunInBridgeMode, nullptr);
  473. const MutexLocker cml(fPlugin->sPluginInfoLoadMutex);
  474. if (carla_load_file(handle, filename))
  475. {
  476. fPluginRunning = true;
  477. fPluginGenericUI = nullptr;
  478. fPluginFilename = filename;
  479. showPluginUI(handle, false);
  480. }
  481. else
  482. {
  483. fPopupError = carla_get_last_error(handle);
  484. d_stdout("got error: %s", fPopupError.buffer());
  485. fDrawingState = kDrawingPluginError;
  486. fPluginFilename.clear();
  487. }
  488. repaint();
  489. }
  490. protected:
  491. void pluginWindowResized(const uint width, const uint height) override
  492. {
  493. const uint extraHeight = kButtonHeight * getScaleFactor() + ImGui::GetStyle().WindowPadding.y * 2;
  494. fNextSize = Size<uint>(width, height + extraHeight);
  495. }
  496. void uiIdle() override
  497. {
  498. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  499. DISTRHO_SAFE_ASSERT_RETURN(handle != nullptr,);
  500. // carla_juce_idle();
  501. if (fDrawingState == kDrawingPluginGenericUI && fPluginGenericUI != nullptr && fPluginHasOutputParameters)
  502. {
  503. updatePluginGenericUI(handle);
  504. repaint();
  505. }
  506. if (fNextSize.isValid())
  507. {
  508. setSize(fNextSize);
  509. fNextSize = Size<uint>();
  510. }
  511. switch (fIdleState)
  512. {
  513. case kIdleInit:
  514. fIdleState = kIdleNothing;
  515. initAndStartRunner();
  516. break;
  517. case kIdleInitPluginAlreadyLoaded:
  518. fIdleState = kIdleNothing;
  519. showPluginUI(handle, false);
  520. initAndStartRunner();
  521. break;
  522. case kIdlePluginLoadedFromDSP:
  523. fIdleState = kIdleNothing;
  524. showPluginUI(handle, false);
  525. break;
  526. case kIdleLoadSelectedPlugin:
  527. fIdleState = kIdleNothing;
  528. loadSelectedPlugin(handle);
  529. break;
  530. case kIdleResetPlugin:
  531. fIdleState = kIdleNothing;
  532. if (fPluginFilename.isNotEmpty())
  533. loadFileAsPlugin(handle, fPluginFilename.buffer());
  534. else
  535. loadPlugin(handle, carla_get_plugin_info(handle, fPluginId)->label);
  536. break;
  537. case kIdleOpenFileUI:
  538. fIdleState = kIdleNothing;
  539. carla_show_custom_ui(handle, fPluginId, true);
  540. break;
  541. case kIdleShowCustomUI:
  542. fIdleState = kIdleNothing;
  543. showPluginUI(handle, true);
  544. break;
  545. case kIdleHideEmbedAndShowGenericUI:
  546. fIdleState = kIdleNothing;
  547. hidePluginUI(handle);
  548. createOrUpdatePluginGenericUI(handle);
  549. break;
  550. case kIdleHidePluginUI:
  551. fIdleState = kIdleNothing;
  552. hidePluginUI(handle);
  553. break;
  554. case kIdleGiveIdleToUI:
  555. if (fPlugin->fCarlaPluginDescriptor->ui_idle != nullptr)
  556. fPlugin->fCarlaPluginDescriptor->ui_idle(fPlugin->fCarlaPluginHandle);
  557. fPluginHostWindow.idle();
  558. break;
  559. case kIdleChangePluginType:
  560. fIdleState = kIdleNothing;
  561. if (fPluginRunning)
  562. hidePluginUI(handle);
  563. if (fNextPluginType == PLUGIN_TYPE_COUNT)
  564. {
  565. FileBrowserOptions opts;
  566. opts.title = "Load from file";
  567. openFileBrowser(opts);
  568. }
  569. else
  570. {
  571. fPluginSelected = -1;
  572. stopRunner();
  573. fPluginType = fNextPluginType;
  574. initAndStartRunner();
  575. }
  576. break;
  577. case kIdleNothing:
  578. break;
  579. }
  580. }
  581. void loadSelectedPlugin(const CarlaHostHandle handle)
  582. {
  583. DISTRHO_SAFE_ASSERT_RETURN(fPluginSelected >= 0,);
  584. const PluginInfoCache& info(fPlugins[fPluginSelected]);
  585. const char* label = nullptr;
  586. switch (fPluginType)
  587. {
  588. case PLUGIN_INTERNAL:
  589. case PLUGIN_AU:
  590. case PLUGIN_JSFX:
  591. case PLUGIN_SFZ:
  592. label = info.label;
  593. break;
  594. case PLUGIN_LV2: {
  595. const char* const slash = std::strchr(info.label, DISTRHO_OS_SEP);
  596. DISTRHO_SAFE_ASSERT_RETURN(slash != nullptr,);
  597. label = slash+1;
  598. break;
  599. }
  600. default:
  601. break;
  602. }
  603. DISTRHO_SAFE_ASSERT_RETURN(label != nullptr,);
  604. d_stdout("Loading %s...", info.name);
  605. loadPlugin(handle, label);
  606. }
  607. void uiFileBrowserSelected(const char* const filename) override
  608. {
  609. if (fPlugin != nullptr && fPlugin->fCarlaHostHandle != nullptr && filename != nullptr)
  610. {
  611. if (fNextPluginType == PLUGIN_TYPE_COUNT)
  612. loadFileAsPlugin(fPlugin->fCarlaHostHandle, filename);
  613. else
  614. carla_set_custom_data(fPlugin->fCarlaHostHandle, fPluginId, CUSTOM_DATA_TYPE_STRING, "file", filename);
  615. }
  616. }
  617. bool initAndStartRunner()
  618. {
  619. if (isRunnerActive())
  620. stopRunner();
  621. fRunnerData.init();
  622. return startRunner();
  623. }
  624. bool run() override
  625. {
  626. if (fRunnerData.needsReinit)
  627. {
  628. fRunnerData.needsReinit = false;
  629. const char* path;
  630. switch (fPluginType)
  631. {
  632. case PLUGIN_LV2:
  633. path = std::getenv("LV2_PATH");
  634. break;
  635. case PLUGIN_JSFX:
  636. path = fPlugin->getPathForJSFX();
  637. break;
  638. default:
  639. path = nullptr;
  640. break;
  641. }
  642. fPluginCount = 0;
  643. delete[] fPlugins;
  644. {
  645. const MutexLocker cml(fPlugin->sPluginInfoLoadMutex);
  646. d_stdout("Will scan plugins now...");
  647. fRunnerData.pluginCount = carla_get_cached_plugin_count(fPluginType, path);
  648. d_stdout("Scanning found %u plugins", fRunnerData.pluginCount);
  649. }
  650. if (fDrawingState == kDrawingLoading)
  651. {
  652. fDrawingState = kDrawingPluginList;
  653. fPluginSearchFirstShow = true;
  654. }
  655. if (fRunnerData.pluginCount != 0)
  656. {
  657. fPlugins = new PluginInfoCache[fRunnerData.pluginCount];
  658. fPluginScanningFinished = false;
  659. return true;
  660. }
  661. else
  662. {
  663. fPlugins = nullptr;
  664. fPluginScanningFinished = true;
  665. return false;
  666. }
  667. }
  668. const uint index = fRunnerData.pluginIndex++;
  669. DISTRHO_SAFE_ASSERT_UINT2_RETURN(index < fRunnerData.pluginCount,
  670. index, fRunnerData.pluginCount, false);
  671. do {
  672. const MutexLocker cml(fPlugin->sPluginInfoLoadMutex);
  673. const CarlaCachedPluginInfo* const info = carla_get_cached_plugin_info(fPluginType, index);
  674. DISTRHO_SAFE_ASSERT_RETURN(info != nullptr, true);
  675. if (! info->valid)
  676. break;
  677. if (info->cvIns != 0 || info->cvOuts != 0)
  678. break;
  679. #ifdef DISTRHO_OS_WASM
  680. if (info->midiIns != 0 && info->midiIns != 1)
  681. break;
  682. if (info->midiOuts != 0 && info->midiOuts != 1)
  683. break;
  684. if (info->audioIns > 2 || info->audioOuts > 2)
  685. break;
  686. if (fPluginType == PLUGIN_INTERNAL)
  687. {
  688. if (std::strcmp(info->label, "audiogain") == 0)
  689. break;
  690. if (std::strcmp(info->label, "midichanfilter") == 0)
  691. break;
  692. if (std::strcmp(info->label, "midichannelize") == 0)
  693. break;
  694. }
  695. #elif DISTRHO_PLUGIN_IS_SYNTH
  696. if (info->midiIns != 1 && info->audioIns != 0)
  697. break;
  698. if ((info->hints & PLUGIN_IS_SYNTH) == 0x0 && info->audioIns != 0)
  699. break;
  700. if (info->audioOuts != 1 && info->audioOuts != 2)
  701. break;
  702. #elif DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  703. if ((info->midiIns != 1 && info->audioIns != 0 && info->audioOuts != 0) || info->midiOuts != 1)
  704. break;
  705. if (info->audioIns != 0 || info->audioOuts != 0)
  706. break;
  707. #else
  708. if (info->audioIns != 1 && info->audioIns != 2)
  709. break;
  710. if (info->audioOuts != 1 && info->audioOuts != 2)
  711. break;
  712. #endif
  713. if (fPluginType == PLUGIN_INTERNAL)
  714. {
  715. #ifndef DISTRHO_OS_WASM
  716. if (std::strcmp(info->label, "audiogain_s") == 0)
  717. break;
  718. #endif
  719. if (std::strcmp(info->label, "lfo") == 0)
  720. break;
  721. if (std::strcmp(info->label, "midi2cv") == 0)
  722. break;
  723. if (std::strcmp(info->label, "midithrough") == 0)
  724. break;
  725. if (std::strcmp(info->label, "3bandsplitter") == 0)
  726. break;
  727. }
  728. const uint pindex = fPluginCount;
  729. fPlugins[pindex].name = strdup(info->name);
  730. fPlugins[pindex].label = strdup(info->label);
  731. ++fPluginCount;
  732. } while (false);
  733. // run again
  734. if (fRunnerData.pluginIndex != fRunnerData.pluginCount)
  735. return true;
  736. // stop here
  737. fPluginScanningFinished = true;
  738. return false;
  739. }
  740. void onImGuiDisplay() override
  741. {
  742. switch (fDrawingState)
  743. {
  744. case kDrawingLoading:
  745. drawLoading();
  746. break;
  747. case kDrawingPluginError:
  748. ImGui::OpenPopup("Plugin Error");
  749. // call ourselves again with the plugin list
  750. fDrawingState = kDrawingPluginList;
  751. onImGuiDisplay();
  752. break;
  753. case kDrawingPluginList:
  754. drawPluginList();
  755. break;
  756. case kDrawingPluginGenericUI:
  757. drawTopBar();
  758. drawGenericUI();
  759. break;
  760. case kDrawingPluginEmbedUI:
  761. drawTopBar();
  762. break;
  763. case kDrawingErrorInit:
  764. fDrawingState = kDrawingErrorDraw;
  765. drawError(true);
  766. break;
  767. case kDrawingErrorDraw:
  768. drawError(false);
  769. break;
  770. }
  771. }
  772. void drawError(const bool open)
  773. {
  774. ImGui::SetNextWindowPos(ImVec2(0, 0));
  775. ImGui::SetNextWindowSize(ImVec2(getWidth(), getHeight()));
  776. const int flags = ImGuiWindowFlags_NoSavedSettings
  777. | ImGuiWindowFlags_NoTitleBar
  778. | ImGuiWindowFlags_NoResize
  779. | ImGuiWindowFlags_NoCollapse
  780. | ImGuiWindowFlags_NoScrollbar
  781. | ImGuiWindowFlags_NoScrollWithMouse;
  782. if (ImGui::Begin("Error Window", nullptr, flags))
  783. {
  784. if (open)
  785. ImGui::OpenPopup("Engine Error");
  786. const int pflags = ImGuiWindowFlags_NoSavedSettings
  787. | ImGuiWindowFlags_NoResize
  788. | ImGuiWindowFlags_NoCollapse
  789. | ImGuiWindowFlags_NoScrollbar
  790. | ImGuiWindowFlags_NoScrollWithMouse
  791. | ImGuiWindowFlags_AlwaysAutoResize
  792. | ImGuiWindowFlags_AlwaysUseWindowPadding;
  793. if (ImGui::BeginPopupModal("Engine Error", nullptr, pflags))
  794. {
  795. ImGui::TextUnformatted(fPopupError.buffer(), nullptr);
  796. ImGui::EndPopup();
  797. }
  798. }
  799. ImGui::End();
  800. }
  801. void drawTopBar()
  802. {
  803. const float padding = ImGui::GetStyle().WindowPadding.y * 2;
  804. ImGui::SetNextWindowPos(ImVec2(0, 0));
  805. ImGui::SetNextWindowSize(ImVec2(getWidth(), kButtonHeight * getScaleFactor() + padding));
  806. const int flags = ImGuiWindowFlags_NoSavedSettings
  807. | ImGuiWindowFlags_NoTitleBar
  808. | ImGuiWindowFlags_NoResize
  809. | ImGuiWindowFlags_NoCollapse
  810. | ImGuiWindowFlags_NoScrollbar
  811. | ImGuiWindowFlags_NoScrollWithMouse;
  812. if (ImGui::Begin("Current Plugin", nullptr, flags))
  813. {
  814. if (ImGui::Button("Pick Another..."))
  815. {
  816. fIdleState = kIdleHidePluginUI;
  817. fDrawingState = kDrawingPluginList;
  818. #ifndef DISTRHO_OS_WASM
  819. const double scaleFactor = getScaleFactor();
  820. fNextSize = Size<uint>(kInitialWidth * scaleFactor, kInitialHeight * scaleFactor);
  821. #endif
  822. }
  823. ImGui::SameLine();
  824. if (ImGui::Button("Reset"))
  825. fIdleState = kIdleResetPlugin;
  826. if (fDrawingState == kDrawingPluginGenericUI)
  827. {
  828. if (fPluginHasCustomUI)
  829. {
  830. ImGui::SameLine();
  831. if (ImGui::Button("Show Custom GUI"))
  832. fIdleState = kIdleShowCustomUI;
  833. }
  834. if (fPluginHasFileOpen)
  835. {
  836. ImGui::SameLine();
  837. if (ImGui::Button("Open File..."))
  838. fIdleState = kIdleOpenFileUI;
  839. }
  840. #ifdef WASM_TESTING
  841. ImGui::SameLine();
  842. ImGui::TextUnformatted(" Plugin to control:");
  843. for (uint i=1; i<10; ++i)
  844. {
  845. char txt[8];
  846. sprintf(txt, "%d", i);
  847. ImGui::SameLine();
  848. if (ImGui::Button(txt))
  849. {
  850. fPluginId = i;
  851. fPluginGenericUI = nullptr;
  852. fIdleState = kIdleHideEmbedAndShowGenericUI;
  853. }
  854. }
  855. #endif
  856. }
  857. if (fDrawingState == kDrawingPluginEmbedUI)
  858. {
  859. ImGui::SameLine();
  860. if (ImGui::Button("Show Generic GUI"))
  861. fIdleState = kIdleHideEmbedAndShowGenericUI;
  862. }
  863. }
  864. ImGui::End();
  865. }
  866. void setupMainWindowPos()
  867. {
  868. const float scaleFactor = getScaleFactor();
  869. float y = 0;
  870. float height = getHeight();
  871. if (fDrawingState == kDrawingPluginGenericUI)
  872. {
  873. y = kButtonHeight * scaleFactor + ImGui::GetStyle().WindowPadding.y * 2 - scaleFactor;
  874. height -= y;
  875. }
  876. ImGui::SetNextWindowPos(ImVec2(0, y));
  877. ImGui::SetNextWindowSize(ImVec2(getWidth(), height));
  878. }
  879. void drawGenericUI()
  880. {
  881. setupMainWindowPos();
  882. PluginGenericUI* const ui = fPluginGenericUI;
  883. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  884. const int pflags = ImGuiWindowFlags_NoSavedSettings
  885. | ImGuiWindowFlags_NoResize
  886. | ImGuiWindowFlags_NoCollapse
  887. | ImGuiWindowFlags_AlwaysAutoResize;
  888. if (ImGui::Begin(ui->title, nullptr, pflags))
  889. {
  890. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  891. for (uint32_t i=0; i < ui->parameterCount; ++i)
  892. {
  893. PluginGenericUI::Parameter& param(ui->parameters[i]);
  894. if (param.readonly)
  895. {
  896. ImGui::BeginDisabled();
  897. ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.printformat,
  898. ImGuiSliderFlags_NoInput | (param.log ? ImGuiSliderFlags_Logarithmic : 0x0));
  899. ImGui::EndDisabled();
  900. continue;
  901. }
  902. if (param.boolean)
  903. {
  904. if (ImGui::Checkbox(param.name, &ui->parameters[i].bvalue))
  905. {
  906. if (ImGui::IsItemActivated())
  907. {
  908. carla_set_parameter_touch(handle, fPluginId, param.rindex, true);
  909. // editParameter(0, true);
  910. }
  911. ui->values[i] = ui->parameters[i].bvalue ? ui->parameters[i].max : ui->parameters[i].min;
  912. carla_set_parameter_value(handle, fPluginId, param.rindex, ui->values[i]);
  913. // setParameterValue(0, ui->values[i]);
  914. }
  915. }
  916. else
  917. {
  918. const bool ret = param.log
  919. ? ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.printformat, ImGuiSliderFlags_Logarithmic)
  920. : ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.printformat);
  921. if (ret)
  922. {
  923. if (ImGui::IsItemActivated())
  924. {
  925. carla_set_parameter_touch(handle, fPluginId, param.rindex, true);
  926. // editParameter(0, true);
  927. }
  928. carla_set_parameter_value(handle, fPluginId, param.rindex, ui->values[i]);
  929. // setParameterValue(0, ui->values[i]);
  930. }
  931. }
  932. if (ImGui::IsItemDeactivated())
  933. {
  934. carla_set_parameter_touch(handle, fPluginId, param.rindex, false);
  935. // editParameter(0, false);
  936. }
  937. }
  938. }
  939. ImGui::End();
  940. }
  941. void drawLoading()
  942. {
  943. setupMainWindowPos();
  944. if (ImGui::Begin("Plugin List", nullptr, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize))
  945. ImGui::TextUnformatted("Loading...", nullptr);
  946. ImGui::End();
  947. }
  948. void drawPluginList()
  949. {
  950. static const char* pluginTypes[] = {
  951. getPluginTypeAsString(PLUGIN_INTERNAL),
  952. getPluginTypeAsString(PLUGIN_LV2),
  953. getPluginTypeAsString(PLUGIN_JSFX),
  954. "Load from file..."
  955. };
  956. setupMainWindowPos();
  957. if (ImGui::Begin("Plugin List", nullptr, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize))
  958. {
  959. const int pflags = ImGuiWindowFlags_NoSavedSettings
  960. | ImGuiWindowFlags_NoResize
  961. | ImGuiWindowFlags_NoCollapse
  962. | ImGuiWindowFlags_NoScrollbar
  963. | ImGuiWindowFlags_NoScrollWithMouse
  964. | ImGuiWindowFlags_AlwaysAutoResize;
  965. if (ImGui::BeginPopupModal("Plugin Error", nullptr, pflags))
  966. {
  967. ImGui::TextWrapped("Failed to load plugin, error was:\n%s", fPopupError.buffer());
  968. ImGui::Separator();
  969. if (ImGui::Button("Ok"))
  970. ImGui::CloseCurrentPopup();
  971. ImGui::SameLine();
  972. ImGui::Dummy(ImVec2(500 * getScaleFactor(), 1));
  973. ImGui::EndPopup();
  974. }
  975. else if (fPluginSearchFirstShow)
  976. {
  977. fPluginSearchFirstShow = false;
  978. ImGui::SetKeyboardFocusHere();
  979. }
  980. if (ImGui::InputText("##pluginsearch", fPluginSearchString, sizeof(fPluginSearchString)-1,
  981. ImGuiInputTextFlags_CharsNoBlank|ImGuiInputTextFlags_AutoSelectAll))
  982. fPluginSearchActive = true;
  983. if (ImGui::IsKeyDown(ImGuiKey_Escape))
  984. fPluginSearchActive = false;
  985. ImGui::SameLine();
  986. ImGui::PushItemWidth(-1.0f);
  987. int current;
  988. switch (fPluginType)
  989. {
  990. case PLUGIN_JSFX:
  991. current = 2;
  992. break;
  993. case PLUGIN_LV2:
  994. current = 1;
  995. break;
  996. default:
  997. current = 0;
  998. break;
  999. }
  1000. if (ImGui::Combo("##plugintypes", &current, pluginTypes, ARRAY_SIZE(pluginTypes)))
  1001. {
  1002. fIdleState = kIdleChangePluginType;
  1003. switch (current)
  1004. {
  1005. case 0:
  1006. fNextPluginType = PLUGIN_INTERNAL;
  1007. break;
  1008. case 1:
  1009. fNextPluginType = PLUGIN_LV2;
  1010. break;
  1011. case 2:
  1012. fNextPluginType = PLUGIN_JSFX;
  1013. break;
  1014. case 3:
  1015. fNextPluginType = PLUGIN_TYPE_COUNT;
  1016. break;
  1017. }
  1018. }
  1019. ImGui::BeginDisabled(!fPluginScanningFinished || fPluginSelected < 0);
  1020. if (ImGui::Button("Load Plugin"))
  1021. fIdleState = kIdleLoadSelectedPlugin;
  1022. // xx cardinal
  1023. if (fPluginType != PLUGIN_INTERNAL /*&& module->canUseBridges*/)
  1024. {
  1025. ImGui::SameLine();
  1026. ImGui::Checkbox("Run in bridge mode", &fPluginWillRunInBridgeMode);
  1027. }
  1028. ImGui::EndDisabled();
  1029. if (fPluginRunning)
  1030. {
  1031. ImGui::SameLine();
  1032. if (ImGui::Button("Cancel"))
  1033. fIdleState = kIdleShowCustomUI;
  1034. }
  1035. if (ImGui::BeginChild("pluginlistwindow"))
  1036. {
  1037. if (ImGui::BeginTable("pluginlist", 2, ImGuiTableFlags_NoSavedSettings))
  1038. {
  1039. const char* const search = fPluginSearchActive && fPluginSearchString[0] != '\0' ? fPluginSearchString : nullptr;
  1040. switch (fPluginType)
  1041. {
  1042. case PLUGIN_INTERNAL:
  1043. case PLUGIN_AU:
  1044. case PLUGIN_SFZ:
  1045. case PLUGIN_JSFX:
  1046. ImGui::TableSetupColumn("Name");
  1047. ImGui::TableSetupColumn("Label");
  1048. ImGui::TableHeadersRow();
  1049. break;
  1050. case PLUGIN_LV2:
  1051. ImGui::TableSetupColumn("Name");
  1052. ImGui::TableSetupColumn("URI");
  1053. ImGui::TableHeadersRow();
  1054. break;
  1055. default:
  1056. break;
  1057. }
  1058. for (uint i=0; i<fPluginCount; ++i)
  1059. {
  1060. const PluginInfoCache& info(fPlugins[i]);
  1061. if (search != nullptr && ildaeil::strcasestr(info.name, search) == nullptr)
  1062. continue;
  1063. bool selected = fPluginSelected >= 0 && static_cast<uint>(fPluginSelected) == i;
  1064. switch (fPluginType)
  1065. {
  1066. case PLUGIN_INTERNAL:
  1067. case PLUGIN_AU:
  1068. case PLUGIN_JSFX:
  1069. case PLUGIN_SFZ:
  1070. ImGui::TableNextRow();
  1071. ImGui::TableSetColumnIndex(0);
  1072. ImGui::Selectable(info.name, &selected);
  1073. ImGui::TableSetColumnIndex(1);
  1074. ImGui::Selectable(info.label, &selected);
  1075. break;
  1076. case PLUGIN_LV2: {
  1077. const char* const slash = std::strchr(info.label, DISTRHO_OS_SEP);
  1078. DISTRHO_SAFE_ASSERT_CONTINUE(slash != nullptr);
  1079. ImGui::TableNextRow();
  1080. ImGui::TableSetColumnIndex(0);
  1081. ImGui::Selectable(info.name, &selected);
  1082. ImGui::TableSetColumnIndex(1);
  1083. ImGui::Selectable(slash+1, &selected);
  1084. break;
  1085. }
  1086. default:
  1087. break;
  1088. }
  1089. if (selected)
  1090. fPluginSelected = i;
  1091. }
  1092. ImGui::EndTable();
  1093. }
  1094. ImGui::EndChild();
  1095. }
  1096. }
  1097. ImGui::End();
  1098. }
  1099. protected:
  1100. /* --------------------------------------------------------------------------------------------------------
  1101. * DSP/Plugin Callbacks */
  1102. void parameterChanged(uint32_t, float) override
  1103. {
  1104. }
  1105. void stateChanged(const char* /* const key */, const char*) override
  1106. {
  1107. /*
  1108. if (std::strcmp(key, "project") == 0)
  1109. hidePluginUI(fPlugin->fCarlaHostHandle);
  1110. */
  1111. }
  1112. // -------------------------------------------------------------------------------------------------------
  1113. private:
  1114. /**
  1115. Set our UI class as non-copyable and add a leak detector just in case.
  1116. */
  1117. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(IldaeilUI)
  1118. };
  1119. // --------------------------------------------------------------------------------------------------------------------
  1120. void ildaeilProjectLoadedFromDSP(void* const ui)
  1121. {
  1122. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1123. static_cast<IldaeilUI*>(ui)->projectLoadedFromDSP();
  1124. }
  1125. void ildaeilParameterChangeForUI(void* const ui, const uint32_t index, const float value)
  1126. {
  1127. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1128. static_cast<IldaeilUI*>(ui)->changeParameterFromDSP(index, value);
  1129. }
  1130. void ildaeilCloseUI(void* ui)
  1131. {
  1132. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  1133. static_cast<IldaeilUI*>(ui)->closeUI();
  1134. }
  1135. const char* ildaeilOpenFileForUI(void* const ui, const bool isDir, const char* const title, const char* const filter)
  1136. {
  1137. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr, nullptr);
  1138. return static_cast<IldaeilUI*>(ui)->openFileFromDSP(isDir, title, filter);
  1139. }
  1140. /* --------------------------------------------------------------------------------------------------------------------
  1141. * UI entry point, called by DPF to create a new UI instance. */
  1142. UI* createUI()
  1143. {
  1144. return new IldaeilUI();
  1145. }
  1146. // --------------------------------------------------------------------------------------------------------------------
  1147. END_NAMESPACE_DISTRHO