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.

955 lines
31KB

  1. /*
  2. * DISTRHO Ildaeil Plugin
  3. * Copyright (C) 2021 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 "CarlaNativePlugin.h"
  18. #include "DistrhoUI.hpp"
  19. #include "DistrhoPlugin.hpp"
  20. #include "PluginHostWindow.hpp"
  21. #include "extra/Thread.hpp"
  22. // IDE helper
  23. #include "DearImGui.hpp"
  24. #include <vector>
  25. START_NAMESPACE_DISTRHO
  26. // --------------------------------------------------------------------------------------------------------------------
  27. class IldaeilPlugin : public Plugin
  28. {
  29. public:
  30. const NativePluginDescriptor* fCarlaPluginDescriptor;
  31. NativePluginHandle fCarlaPluginHandle;
  32. NativeHostDescriptor fCarlaHostDescriptor;
  33. CarlaHostHandle fCarlaHostHandle;
  34. void* fUI;
  35. // ...
  36. };
  37. // --------------------------------------------------------------------------------------------------------------------
  38. void ildaeilParameterChangeForUI(void* ui, uint32_t index, float value);
  39. const char* ildaeilOpenFileForUI(void* ui, bool isDir, const char* title, const char* filter);
  40. // --------------------------------------------------------------------------------------------------------------------
  41. using namespace CarlaBackend;
  42. // shared resource pointer
  43. // carla_juce_init();
  44. class IldaeilUI : public UI,
  45. public Thread,
  46. public PluginHostWindow::Callbacks
  47. {
  48. static constexpr const uint kInitialWidth = 1220;
  49. static constexpr const uint kInitialHeight = 640;
  50. static constexpr const uint kGenericWidth = 360;
  51. static constexpr const uint kGenericHeight = 400;
  52. static constexpr const uint kButtonHeight = 20;
  53. enum {
  54. kDrawingInit,
  55. kDrawingErrorInit,
  56. kDrawingErrorDraw,
  57. kDrawingLoading,
  58. kDrawingPluginList,
  59. kDrawingPluginEmbedUI,
  60. kDrawingPluginGenericUI,
  61. kDrawingPluginPendingFromInit
  62. } fDrawingState;
  63. struct PluginInfoCache {
  64. char* name;
  65. char* label;
  66. PluginInfoCache()
  67. : name(nullptr),
  68. label(nullptr) {}
  69. ~PluginInfoCache()
  70. {
  71. std::free(name);
  72. std::free(label);
  73. }
  74. };
  75. struct PluginGenericUI {
  76. char* title;
  77. uint parameterCount;
  78. struct Parameter {
  79. char* name;
  80. char* format;
  81. uint32_t rindex;
  82. bool boolean, bvalue, log;
  83. float min, max, power;
  84. Parameter()
  85. : name(nullptr),
  86. format(nullptr),
  87. rindex(0),
  88. boolean(false),
  89. bvalue(false),
  90. log(false),
  91. min(0.0f),
  92. max(1.0f) {}
  93. ~Parameter()
  94. {
  95. std::free(name);
  96. std::free(format);
  97. }
  98. }* parameters;
  99. float* values;
  100. PluginGenericUI()
  101. : title(nullptr),
  102. parameterCount(0),
  103. parameters(nullptr),
  104. values(nullptr) {}
  105. ~PluginGenericUI()
  106. {
  107. std::free(title);
  108. delete[] parameters;
  109. delete[] values;
  110. }
  111. };
  112. IldaeilPlugin* const fPlugin;
  113. PluginHostWindow fPluginHostWindow;
  114. PluginType fPluginType;
  115. uint fPluginCount;
  116. uint fPluginSelected;
  117. bool fPluginScanningFinished;
  118. bool fPluginHasCustomUI;
  119. bool fPluginHasEmbedUI;
  120. bool fPluginWillRunInBridgeMode;
  121. PluginInfoCache* fPlugins;
  122. ScopedPointer<PluginGenericUI> fPluginGenericUI;
  123. bool fPluginSearchActive;
  124. bool fPluginSearchFirstShow;
  125. char fPluginSearchString[0xff];
  126. String fPopupError;
  127. public:
  128. IldaeilUI()
  129. : UI(kInitialWidth, kInitialHeight),
  130. Thread("IldaeilScanner"),
  131. fDrawingState(kDrawingInit),
  132. fPlugin((IldaeilPlugin*)getPluginInstancePointer()),
  133. fPluginHostWindow(getWindow(), this),
  134. fPluginType(PLUGIN_LV2),
  135. fPluginCount(0),
  136. fPluginSelected(0),
  137. fPluginScanningFinished(false),
  138. fPluginHasCustomUI(false),
  139. fPluginHasEmbedUI(false),
  140. fPluginWillRunInBridgeMode(false),
  141. fPlugins(nullptr),
  142. fPluginSearchActive(false),
  143. fPluginSearchFirstShow(false)
  144. {
  145. const double scaleFactor = getScaleFactor();
  146. if (fPlugin == nullptr || fPlugin->fCarlaHostHandle == nullptr)
  147. {
  148. fDrawingState = kDrawingErrorInit;
  149. fPopupError = "Ildaeil backend failed to init properly, cannot continue.";
  150. setSize(kInitialWidth * scaleFactor * 0.5, kInitialHeight * scaleFactor * 0.5);
  151. return;
  152. }
  153. std::strcpy(fPluginSearchString, "Search...");
  154. ImGuiStyle& style(ImGui::GetStyle());
  155. style.FrameRounding = 4;
  156. const double padding = style.WindowPadding.y * 2;
  157. if (d_isNotEqual(scaleFactor, 1.0))
  158. {
  159. setSize(kInitialWidth * scaleFactor, kInitialHeight * scaleFactor);
  160. fPluginHostWindow.setPositionAndSize(0, kButtonHeight * scaleFactor + padding,
  161. kInitialWidth * scaleFactor,
  162. (kInitialHeight - kButtonHeight) * scaleFactor - padding);
  163. }
  164. else
  165. {
  166. fPluginHostWindow.setPositionAndSize(0, kButtonHeight + padding,
  167. kInitialWidth, kInitialHeight - kButtonHeight - padding);
  168. }
  169. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  170. char winIdStr[24];
  171. std::snprintf(winIdStr, sizeof(winIdStr), "%lx", (ulong)getWindow().getNativeWindowHandle());
  172. carla_set_engine_option(handle, ENGINE_OPTION_FRONTEND_WIN_ID, 0, winIdStr);
  173. carla_set_engine_option(handle, ENGINE_OPTION_FRONTEND_UI_SCALE, getScaleFactor()*1000, nullptr);
  174. if (carla_get_current_plugin_count(handle) != 0)
  175. {
  176. const uint hints = carla_get_plugin_info(handle, 0)->hints;
  177. fDrawingState = kDrawingPluginPendingFromInit;
  178. fPluginHasCustomUI = hints & PLUGIN_HAS_CUSTOM_UI;
  179. fPluginHasEmbedUI = hints & PLUGIN_HAS_CUSTOM_EMBED_UI;
  180. }
  181. fPlugin->fUI = this;
  182. }
  183. ~IldaeilUI() override
  184. {
  185. if (fPlugin != nullptr && fPlugin->fCarlaHostHandle != nullptr)
  186. {
  187. fPlugin->fUI = nullptr;
  188. carla_set_engine_option(fPlugin->fCarlaHostHandle, ENGINE_OPTION_FRONTEND_WIN_ID, 0, "0");
  189. }
  190. if (isThreadRunning())
  191. stopThread(-1);
  192. hidePluginUI();
  193. fPluginGenericUI = nullptr;
  194. delete[] fPlugins;
  195. }
  196. void changeParameterFromDSP(const uint32_t index, const float value)
  197. {
  198. if (PluginGenericUI* const ui = fPluginGenericUI)
  199. {
  200. for (uint32_t i=0; i < ui->parameterCount; ++i)
  201. {
  202. if (ui->parameters[i].rindex != index)
  203. continue;
  204. ui->values[i] = value;
  205. if (ui->parameters[i].boolean)
  206. ui->parameters[i].bvalue = value > ui->parameters[i].min;
  207. break;
  208. }
  209. }
  210. }
  211. const char* openFileFromDSP(const bool /*isDir*/, const char* const title, const char* const /*filter*/)
  212. {
  213. Window::FileBrowserOptions opts;
  214. opts.title = title;
  215. getWindow().openFileBrowser(opts);
  216. return nullptr;
  217. }
  218. void showPluginUI(const CarlaHostHandle handle)
  219. {
  220. const CarlaPluginInfo* const info = carla_get_plugin_info(handle, 0);
  221. if (info->hints & PLUGIN_HAS_CUSTOM_EMBED_UI)
  222. {
  223. fDrawingState = kDrawingPluginEmbedUI;
  224. fPluginHasCustomUI = true;
  225. fPluginHasEmbedUI = true;
  226. carla_embed_custom_ui(handle, 0, fPluginHostWindow.attachAndGetWindowHandle());
  227. }
  228. else
  229. {
  230. fDrawingState = kDrawingPluginGenericUI;
  231. fPluginHasCustomUI = info->hints & PLUGIN_HAS_CUSTOM_UI;
  232. fPluginHasEmbedUI = false;
  233. if (fPluginGenericUI == nullptr)
  234. createPluginGenericUI(handle, info);
  235. else
  236. updatePluginGenericUI(handle);
  237. ImGuiStyle& style(ImGui::GetStyle());
  238. const double scaleFactor = getScaleFactor();
  239. setSize(kGenericWidth * scaleFactor, (kGenericHeight + style.FramePadding.x) * scaleFactor);
  240. }
  241. repaint();
  242. }
  243. void hidePluginUI()
  244. {
  245. if (fPlugin == nullptr || fPlugin->fCarlaHostHandle == nullptr)
  246. return;
  247. fPluginHostWindow.hide();
  248. if (fDrawingState == kDrawingPluginGenericUI || fDrawingState == kDrawingPluginEmbedUI)
  249. carla_show_custom_ui(fPlugin->fCarlaHostHandle, 0, false);
  250. }
  251. void createPluginGenericUI(const CarlaHostHandle handle, const CarlaPluginInfo* const info)
  252. {
  253. PluginGenericUI* const ui = new PluginGenericUI;
  254. String title(info->name);
  255. title += " by ";
  256. title += info->maker;
  257. ui->title = title.getAndReleaseBuffer();
  258. const uint32_t pcount = ui->parameterCount = carla_get_parameter_count(handle, 0);
  259. // make count of valid parameters
  260. for (uint32_t i=0; i < pcount; ++i)
  261. {
  262. const ParameterData* const pdata = carla_get_parameter_data(handle, 0, i);
  263. if (pdata->type != PARAMETER_INPUT ||
  264. (pdata->hints & PARAMETER_IS_ENABLED) == 0x0 ||
  265. (pdata->hints & PARAMETER_IS_READ_ONLY) != 0x0)
  266. {
  267. --ui->parameterCount;
  268. continue;
  269. }
  270. }
  271. ui->parameters = new PluginGenericUI::Parameter[ui->parameterCount];
  272. ui->values = new float[ui->parameterCount];
  273. // now safely fill in details
  274. for (uint32_t i=0, j=0; i < pcount; ++i)
  275. {
  276. const ParameterData* const pdata = carla_get_parameter_data(handle, 0, i);
  277. if (pdata->type != PARAMETER_INPUT ||
  278. (pdata->hints & PARAMETER_IS_ENABLED) == 0x0 ||
  279. (pdata->hints & PARAMETER_IS_READ_ONLY) != 0x0)
  280. continue;
  281. const CarlaParameterInfo* const pinfo = carla_get_parameter_info(handle, 0, i);
  282. const ::ParameterRanges* const pranges = carla_get_parameter_ranges(handle, 0, i);
  283. String format;
  284. if (pdata->hints & PARAMETER_IS_INTEGER)
  285. format = "%.0f ";
  286. else
  287. format = "%.3f ";
  288. format += pinfo->unit;
  289. PluginGenericUI::Parameter& param(ui->parameters[j]);
  290. param.name = strdup(pinfo->name);
  291. param.format = format.getAndReleaseBuffer();
  292. param.rindex = i;
  293. param.boolean = pdata->hints & PARAMETER_IS_BOOLEAN;
  294. param.log = pdata->hints & PARAMETER_IS_LOGARITHMIC;
  295. param.min = pranges->min;
  296. param.max = pranges->max;
  297. ui->values[j] = carla_get_current_parameter_value(handle, 0, i);
  298. if (param.boolean)
  299. param.bvalue = ui->values[j] > param.min;
  300. else
  301. param.bvalue = false;
  302. ++j;
  303. }
  304. fPluginGenericUI = ui;
  305. }
  306. void updatePluginGenericUI(const CarlaHostHandle handle)
  307. {
  308. PluginGenericUI* const ui = fPluginGenericUI;
  309. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  310. for (uint32_t i=0; i < ui->parameterCount; ++i)
  311. {
  312. ui->values[i] = carla_get_current_parameter_value(handle, 0, ui->parameters[i].rindex);
  313. if (ui->parameters[i].boolean)
  314. ui->parameters[i].bvalue = ui->values[i] > ui->parameters[i].min;
  315. }
  316. }
  317. bool loadPlugin(const CarlaHostHandle handle, const char* const label)
  318. {
  319. if (carla_get_current_plugin_count(handle) != 0)
  320. {
  321. hidePluginUI();
  322. carla_replace_plugin(handle, 0);
  323. }
  324. carla_set_engine_option(handle, ENGINE_OPTION_PREFER_PLUGIN_BRIDGES, fPluginWillRunInBridgeMode, nullptr);
  325. if (carla_add_plugin(handle, BINARY_NATIVE, fPluginType, nullptr, nullptr,
  326. label, 0, 0x0, PLUGIN_OPTIONS_NULL))
  327. {
  328. fPluginGenericUI = nullptr;
  329. showPluginUI(handle);
  330. return true;
  331. }
  332. else
  333. {
  334. fPopupError = carla_get_last_error(handle);
  335. d_stdout("got error: %s", fPopupError.buffer());
  336. ImGui::OpenPopup("Plugin Error");
  337. }
  338. return false;
  339. }
  340. protected:
  341. void pluginWindowResized(uint width, uint height) override
  342. {
  343. setSize(width, height + kButtonHeight * getScaleFactor() + ImGui::GetStyle().WindowPadding.y * 2);
  344. }
  345. void uiIdle() override
  346. {
  347. switch (fDrawingState)
  348. {
  349. case kDrawingInit:
  350. fDrawingState = kDrawingLoading;
  351. startThread();
  352. repaint();
  353. break;
  354. case kDrawingPluginPendingFromInit:
  355. showPluginUI(fPlugin->fCarlaHostHandle);
  356. startThread();
  357. break;
  358. case kDrawingPluginEmbedUI:
  359. fPlugin->fCarlaPluginDescriptor->ui_idle(fPlugin->fCarlaPluginHandle);
  360. fPluginHostWindow.idle();
  361. break;
  362. case kDrawingPluginGenericUI:
  363. fPlugin->fCarlaPluginDescriptor->ui_idle(fPlugin->fCarlaPluginHandle);
  364. break;
  365. default:
  366. break;
  367. }
  368. }
  369. void uiFileBrowserSelected(const char* const filename) override
  370. {
  371. if (fPlugin != nullptr && fPlugin->fCarlaHostHandle != nullptr && filename != nullptr)
  372. carla_set_custom_data(fPlugin->fCarlaHostHandle, 0, CUSTOM_DATA_TYPE_STRING, "file", filename);
  373. }
  374. void run() override
  375. {
  376. if (const uint count = carla_get_cached_plugin_count(fPluginType, nullptr))
  377. {
  378. fPluginCount = 0;
  379. fPlugins = new PluginInfoCache[count];
  380. if (fDrawingState == kDrawingLoading)
  381. {
  382. fDrawingState = kDrawingPluginList;
  383. fPluginSearchFirstShow = true;
  384. }
  385. for (uint i=0, j; i < count && ! shouldThreadExit(); ++i)
  386. {
  387. const CarlaCachedPluginInfo* const info = carla_get_cached_plugin_info(fPluginType, i);
  388. DISTRHO_SAFE_ASSERT_CONTINUE(info != nullptr);
  389. if (! info->valid)
  390. continue;
  391. #if DISTRHO_PLUGIN_IS_SYNTH
  392. if (info->midiIns != 1 || (info->audioOuts != 1 && info->audioOuts != 2))
  393. continue;
  394. if ((info->hints & PLUGIN_IS_SYNTH) == 0x0)
  395. continue;
  396. #elif DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  397. if (info->midiIns != 1 || info->midiOuts != 1)
  398. continue;
  399. if (info->audioIns != 0 || info->audioOuts != 0)
  400. continue;
  401. #else
  402. if (info->audioIns != 2 || info->audioOuts != 2)
  403. continue;
  404. #endif
  405. j = fPluginCount;
  406. fPlugins[j].name = strdup(info->name);
  407. fPlugins[j].label = strdup(info->label);
  408. ++fPluginCount;
  409. }
  410. }
  411. if (! shouldThreadExit())
  412. fPluginScanningFinished = true;
  413. }
  414. void onImGuiDisplay() override
  415. {
  416. switch (fDrawingState)
  417. {
  418. case kDrawingInit:
  419. case kDrawingLoading:
  420. case kDrawingPluginPendingFromInit:
  421. drawLoading();
  422. break;
  423. case kDrawingPluginList:
  424. drawPluginList();
  425. break;
  426. case kDrawingErrorInit:
  427. fDrawingState = kDrawingErrorDraw;
  428. drawError(true);
  429. break;
  430. case kDrawingErrorDraw:
  431. drawError(false);
  432. break;
  433. case kDrawingPluginGenericUI:
  434. drawGenericUI();
  435. // fall-through
  436. case kDrawingPluginEmbedUI:
  437. drawTopBar();
  438. break;
  439. }
  440. }
  441. void drawError(const bool open)
  442. {
  443. ImGui::SetNextWindowPos(ImVec2(0, 0));
  444. ImGui::SetNextWindowSize(ImVec2(getWidth(), getHeight()));
  445. const int flags = ImGuiWindowFlags_NoSavedSettings
  446. | ImGuiWindowFlags_NoTitleBar
  447. | ImGuiWindowFlags_NoResize
  448. | ImGuiWindowFlags_NoCollapse
  449. | ImGuiWindowFlags_NoScrollbar
  450. | ImGuiWindowFlags_NoScrollWithMouse
  451. | ImGuiWindowFlags_NoCollapse;
  452. if (ImGui::Begin("Error Window", nullptr, flags))
  453. {
  454. if (open)
  455. ImGui::OpenPopup("Engine Error");
  456. const int pflags = ImGuiWindowFlags_NoSavedSettings
  457. | ImGuiWindowFlags_NoResize
  458. | ImGuiWindowFlags_NoCollapse
  459. | ImGuiWindowFlags_NoScrollbar
  460. | ImGuiWindowFlags_NoScrollWithMouse
  461. | ImGuiWindowFlags_NoCollapse
  462. | ImGuiWindowFlags_AlwaysAutoResize
  463. | ImGuiWindowFlags_AlwaysUseWindowPadding;
  464. if (ImGui::BeginPopupModal("Engine Error", nullptr, pflags))
  465. {
  466. ImGui::TextUnformatted(fPopupError.buffer(), nullptr);
  467. ImGui::EndPopup();
  468. }
  469. }
  470. ImGui::End();
  471. }
  472. void drawTopBar()
  473. {
  474. const float padding = ImGui::GetStyle().WindowPadding.y * 2;
  475. ImGui::SetNextWindowPos(ImVec2(0, 0));
  476. ImGui::SetNextWindowSize(ImVec2(getWidth(), kButtonHeight * getScaleFactor() + padding));
  477. const int flags = ImGuiWindowFlags_NoSavedSettings
  478. | ImGuiWindowFlags_NoTitleBar
  479. | ImGuiWindowFlags_NoResize
  480. | ImGuiWindowFlags_NoCollapse
  481. | ImGuiWindowFlags_NoScrollbar
  482. | ImGuiWindowFlags_NoScrollWithMouse
  483. | ImGuiWindowFlags_NoCollapse;
  484. if (ImGui::Begin("Current Plugin", nullptr, flags))
  485. {
  486. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  487. if (ImGui::Button("Pick Another..."))
  488. {
  489. hidePluginUI();
  490. fDrawingState = kDrawingPluginList;
  491. const double scaleFactor = getScaleFactor();
  492. setSize(kInitialWidth * scaleFactor, kInitialHeight * scaleFactor);
  493. }
  494. ImGui::SameLine();
  495. if (ImGui::Button("Reset"))
  496. {
  497. loadPlugin(handle, carla_get_plugin_info(handle, 0)->label);
  498. }
  499. if (fDrawingState == kDrawingPluginGenericUI && fPluginHasCustomUI)
  500. {
  501. ImGui::SameLine();
  502. if (ImGui::Button("Show Custom GUI"))
  503. {
  504. if (fPluginHasEmbedUI)
  505. {
  506. fDrawingState = kDrawingPluginEmbedUI;
  507. carla_embed_custom_ui(handle, 0, fPluginHostWindow.attachAndGetWindowHandle());
  508. }
  509. else
  510. {
  511. carla_show_custom_ui(handle, 0, true);
  512. }
  513. ImGui::End();
  514. return;
  515. }
  516. }
  517. if (fDrawingState == kDrawingPluginEmbedUI)
  518. {
  519. ImGui::SameLine();
  520. if (ImGui::Button("Show Generic GUI"))
  521. {
  522. hidePluginUI();
  523. fDrawingState = kDrawingPluginGenericUI;
  524. if (fPluginGenericUI == nullptr)
  525. createPluginGenericUI(handle, carla_get_plugin_info(handle, 0));
  526. else
  527. updatePluginGenericUI(handle);
  528. const double scaleFactor = getScaleFactor();
  529. const double padding = ImGui::GetStyle().WindowPadding.y * 2;
  530. setSize(std::max(getWidth(), static_cast<uint>(kGenericWidth * scaleFactor + 0.5)),
  531. (kGenericHeight + kButtonHeight) * scaleFactor + padding);
  532. }
  533. }
  534. }
  535. ImGui::End();
  536. }
  537. void setupMainWindowPos()
  538. {
  539. float y = 0;
  540. float height = getHeight();
  541. if (fDrawingState == kDrawingPluginGenericUI)
  542. {
  543. y = kButtonHeight * getScaleFactor() + ImGui::GetStyle().WindowPadding.y * 2 - 1;
  544. height -= y;
  545. }
  546. ImGui::SetNextWindowPos(ImVec2(0, y));
  547. ImGui::SetNextWindowSize(ImVec2(getWidth(), height));
  548. }
  549. void drawGenericUI()
  550. {
  551. setupMainWindowPos();
  552. PluginGenericUI* const ui = fPluginGenericUI;
  553. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  554. // ImGui::SetNextWindowFocus();
  555. if (ImGui::Begin(ui->title, nullptr, ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoCollapse))
  556. {
  557. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  558. for (uint32_t i=0; i < ui->parameterCount; ++i)
  559. {
  560. PluginGenericUI::Parameter& param(ui->parameters[i]);
  561. if (param.boolean)
  562. {
  563. if (ImGui::Checkbox(param.name, &ui->parameters[i].bvalue))
  564. {
  565. if (ImGui::IsItemActivated())
  566. {
  567. carla_set_parameter_touch(handle, 0, param.rindex, true);
  568. // editParameter(0, true);
  569. }
  570. ui->values[i] = ui->parameters[i].bvalue ? ui->parameters[i].max : ui->parameters[i].min;
  571. carla_set_parameter_value(handle, 0, param.rindex, ui->values[i]);
  572. // setParameterValue(0, ui->values[i]);
  573. }
  574. }
  575. else
  576. {
  577. const bool ret = param.log
  578. ? ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.format, 2.0f)
  579. : ImGui::SliderFloat(param.name, &ui->values[i], param.min, param.max, param.format);
  580. if (ret)
  581. {
  582. if (ImGui::IsItemActivated())
  583. {
  584. carla_set_parameter_touch(handle, 0, param.rindex, true);
  585. // editParameter(0, true);
  586. }
  587. carla_set_parameter_value(handle, 0, param.rindex, ui->values[i]);
  588. // setParameterValue(0, ui->values[i]);
  589. }
  590. }
  591. if (ImGui::IsItemDeactivated())
  592. {
  593. carla_set_parameter_touch(handle, 0, param.rindex, false);
  594. // editParameter(0, false);
  595. }
  596. }
  597. }
  598. ImGui::End();
  599. }
  600. void drawLoading()
  601. {
  602. setupMainWindowPos();
  603. if (ImGui::Begin("Plugin List", nullptr, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize))
  604. {
  605. ImGui::TextUnformatted("Loading...", nullptr);
  606. }
  607. ImGui::End();
  608. }
  609. void drawPluginList()
  610. {
  611. setupMainWindowPos();
  612. const CarlaHostHandle handle = fPlugin->fCarlaHostHandle;
  613. if (ImGui::Begin("Plugin List", nullptr, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize))
  614. {
  615. const int pflags = ImGuiWindowFlags_NoSavedSettings
  616. | ImGuiWindowFlags_NoResize
  617. | ImGuiWindowFlags_NoCollapse
  618. | ImGuiWindowFlags_NoScrollbar
  619. | ImGuiWindowFlags_NoScrollWithMouse
  620. | ImGuiWindowFlags_NoCollapse
  621. | ImGuiWindowFlags_AlwaysAutoResize
  622. | ImGuiWindowFlags_AlwaysUseWindowPadding;
  623. if (ImGui::BeginPopupModal("Plugin Error", nullptr, pflags))
  624. {
  625. ImGui::TextWrapped("Failed to load plugin, error was:\n%s", fPopupError.buffer());
  626. ImGui::Separator();
  627. if (ImGui::Button("Ok"))
  628. {
  629. ImGui::CloseCurrentPopup();
  630. }
  631. ImGui::SameLine();
  632. ImGui::Dummy(ImVec2(500 * getScaleFactor(), 1));
  633. ImGui::EndPopup();
  634. }
  635. else if (fPluginSearchFirstShow)
  636. {
  637. fPluginSearchFirstShow = false;
  638. ImGui::SetKeyboardFocusHere();
  639. }
  640. if (ImGui::InputText("", fPluginSearchString, sizeof(fPluginSearchString)-1, ImGuiInputTextFlags_CharsNoBlank|ImGuiInputTextFlags_AutoSelectAll))
  641. fPluginSearchActive = true;
  642. if (ImGui::IsKeyDown(ImGuiKey_Escape))
  643. fPluginSearchActive = false;
  644. ImGui::BeginDisabled(!fPluginScanningFinished);
  645. if (ImGui::Button("Load Plugin"))
  646. {
  647. do {
  648. const PluginInfoCache& info(fPlugins[fPluginSelected]);
  649. const char* label = nullptr;
  650. switch (fPluginType)
  651. {
  652. case PLUGIN_INTERNAL:
  653. label = info.label;
  654. break;
  655. case PLUGIN_LV2: {
  656. const char* const slash = std::strchr(info.label, DISTRHO_OS_SEP);
  657. DISTRHO_SAFE_ASSERT_BREAK(slash != nullptr);
  658. label = slash+1;
  659. break;
  660. }
  661. default:
  662. break;
  663. }
  664. DISTRHO_SAFE_ASSERT_BREAK(label != nullptr);
  665. d_stdout("Loading %s...", info.name);
  666. if (loadPlugin(handle, label))
  667. {
  668. ImGui::EndDisabled();
  669. ImGui::End();
  670. return;
  671. }
  672. } while (false);
  673. }
  674. ImGui::SameLine();
  675. ImGui::Checkbox("Run in bridge mode", &fPluginWillRunInBridgeMode);
  676. if (carla_get_current_plugin_count(handle) != 0)
  677. {
  678. ImGui::SameLine();
  679. if (ImGui::Button("Cancel"))
  680. {
  681. showPluginUI(handle);
  682. }
  683. }
  684. ImGui::EndDisabled();
  685. if (ImGui::BeginChild("pluginlistwindow"))
  686. {
  687. if (ImGui::BeginTable("pluginlist",
  688. fPluginType == PLUGIN_LV2 ? 3 : 2, ImGuiTableFlags_NoSavedSettings))
  689. {
  690. const char* const search = fPluginSearchActive && fPluginSearchString[0] != '\0' ? fPluginSearchString : nullptr;
  691. switch (fPluginType)
  692. {
  693. case PLUGIN_INTERNAL:
  694. ImGui::TableSetupColumn("Name");
  695. ImGui::TableSetupColumn("Label");
  696. ImGui::TableHeadersRow();
  697. break;
  698. case PLUGIN_LV2:
  699. ImGui::TableSetupColumn("Name");
  700. ImGui::TableSetupColumn("Bundle");
  701. ImGui::TableSetupColumn("URI");
  702. ImGui::TableHeadersRow();
  703. break;
  704. default:
  705. break;
  706. }
  707. for (uint i=0; i<fPluginCount; ++i)
  708. {
  709. const PluginInfoCache& info(fPlugins[i]);
  710. if (search != nullptr && strcasestr(info.name, search) == nullptr)
  711. continue;
  712. bool selected = fPluginSelected == i;
  713. switch (fPluginType)
  714. {
  715. case PLUGIN_INTERNAL:
  716. ImGui::TableNextRow();
  717. ImGui::TableSetColumnIndex(0);
  718. ImGui::Selectable(info.name, &selected);
  719. ImGui::TableSetColumnIndex(1);
  720. ImGui::Selectable(info.label, &selected);
  721. break;
  722. case PLUGIN_LV2: {
  723. const char* const slash = std::strchr(info.label, DISTRHO_OS_SEP);
  724. DISTRHO_SAFE_ASSERT_CONTINUE(slash != nullptr);
  725. ImGui::TableNextRow();
  726. ImGui::TableSetColumnIndex(0);
  727. ImGui::Selectable(info.name, &selected);
  728. ImGui::TableSetColumnIndex(1);
  729. ImGui::Selectable(slash+1, &selected);
  730. ImGui::TableSetColumnIndex(2);
  731. ImGui::TextUnformatted(info.label, slash);
  732. break;
  733. }
  734. default:
  735. break;
  736. }
  737. if (selected)
  738. fPluginSelected = i;
  739. }
  740. ImGui::EndTable();
  741. }
  742. ImGui::EndChild();
  743. }
  744. }
  745. ImGui::End();
  746. }
  747. protected:
  748. /* --------------------------------------------------------------------------------------------------------
  749. * DSP/Plugin Callbacks */
  750. void parameterChanged(uint32_t, float) override
  751. {
  752. }
  753. void stateChanged(const char* const key, const char* const) override
  754. {
  755. if (std::strcmp(key, "project") == 0)
  756. hidePluginUI();
  757. }
  758. // -------------------------------------------------------------------------------------------------------
  759. private:
  760. /**
  761. Set our UI class as non-copyable and add a leak detector just in case.
  762. */
  763. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(IldaeilUI)
  764. };
  765. // --------------------------------------------------------------------------------------------------------------------
  766. void ildaeilParameterChangeForUI(void* const ui, const uint32_t index, const float value)
  767. {
  768. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr,);
  769. static_cast<IldaeilUI*>(ui)->changeParameterFromDSP(index, value);
  770. }
  771. const char* ildaeilOpenFileForUI(void* const ui, const bool isDir, const char* const title, const char* const filter)
  772. {
  773. DISTRHO_SAFE_ASSERT_RETURN(ui != nullptr, nullptr);
  774. return static_cast<IldaeilUI*>(ui)->openFileFromDSP(isDir, title, filter);
  775. }
  776. /* --------------------------------------------------------------------------------------------------------------------
  777. * UI entry point, called by DPF to create a new UI instance. */
  778. UI* createUI()
  779. {
  780. return new IldaeilUI();
  781. }
  782. // --------------------------------------------------------------------------------------------------------------------
  783. END_NAMESPACE_DISTRHO