Audio plugin host https://kx.studio/carla
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.

873 lines
25KB

  1. /*
  2. * Carla LV2 Single Plugin
  3. * Copyright (C) 2017 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 doc/GPL.txt file.
  16. */
  17. #ifndef BUILD_BRIDGE
  18. # error This file should not be compiled if not building bridge
  19. #endif
  20. #include "engine/CarlaEngineInternal.hpp"
  21. #include "CarlaPlugin.hpp"
  22. #include "CarlaBackendUtils.hpp"
  23. #include "CarlaEngineUtils.hpp"
  24. #include "CarlaLv2Utils.hpp"
  25. #include "CarlaUtils.h"
  26. #include "AppConfig.h"
  27. #include "juce_core/juce_core.h"
  28. // ---------------------------------------------------------------------------------------------------------------------
  29. // -Weffc++ compat ext widget
  30. extern "C" {
  31. typedef struct _LV2_External_UI_Widget_Compat {
  32. void (*run )(struct _LV2_External_UI_Widget_Compat*);
  33. void (*show)(struct _LV2_External_UI_Widget_Compat*);
  34. void (*hide)(struct _LV2_External_UI_Widget_Compat*);
  35. _LV2_External_UI_Widget_Compat() noexcept
  36. : run(nullptr), show(nullptr), hide(nullptr) {}
  37. } LV2_External_UI_Widget_Compat;
  38. }
  39. // ---------------------------------------------------------------------------------------------------------------------
  40. CARLA_BACKEND_START_NAMESPACE
  41. #if defined(__clang__)
  42. # pragma clang diagnostic push
  43. # pragma clang diagnostic ignored "-Weffc++"
  44. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  45. # pragma GCC diagnostic push
  46. # pragma GCC diagnostic ignored "-Weffc++"
  47. #endif
  48. class CarlaEngineLV2Single : public CarlaEngine,
  49. public LV2_External_UI_Widget_Compat
  50. {
  51. #if defined(__clang__)
  52. # pragma clang diagnostic pop
  53. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  54. # pragma GCC diagnostic pop
  55. #endif
  56. public:
  57. CarlaEngineLV2Single(const uint32_t bufferSize, const double sampleRate, const char* const bundlePath, const LV2_URID_Map* uridMap)
  58. : fPlugin(nullptr),
  59. fIsActive(false),
  60. fIsOffline(false),
  61. fPorts(),
  62. fUI()
  63. {
  64. run = extui_run;
  65. show = extui_show;
  66. hide = extui_hide;
  67. CARLA_SAFE_ASSERT_RETURN(pData->curPluginCount == 0,)
  68. CARLA_SAFE_ASSERT_RETURN(pData->plugins[0].plugin == nullptr,);
  69. // xxxxx
  70. CarlaString binaryDir(bundlePath);
  71. binaryDir += CARLA_OS_SEP_STR "bin" CARLA_OS_SEP_STR;
  72. CarlaString resourceDir(bundlePath);
  73. resourceDir += CARLA_OS_SEP_STR "res" CARLA_OS_SEP_STR;
  74. pData->bufferSize = bufferSize;
  75. pData->sampleRate = sampleRate;
  76. pData->initTime(nullptr);
  77. pData->options.processMode = ENGINE_PROCESS_MODE_BRIDGE;
  78. pData->options.transportMode = ENGINE_TRANSPORT_MODE_PLUGIN;
  79. pData->options.forceStereo = false;
  80. pData->options.preferPluginBridges = false;
  81. pData->options.preferUiBridges = false;
  82. init("LV2-Export");
  83. if (pData->options.resourceDir != nullptr)
  84. delete[] pData->options.resourceDir;
  85. if (pData->options.binaryDir != nullptr)
  86. delete[] pData->options.binaryDir;
  87. pData->options.binaryDir = binaryDir.dup();
  88. pData->options.resourceDir = resourceDir.dup();
  89. setCallback(_engine_callback, this);
  90. using juce::File;
  91. const File pluginFile(File::getSpecialLocation(File::currentExecutableFile).withFileExtension("xml"));
  92. if (! loadProject(pluginFile.getFullPathName().toRawUTF8()))
  93. {
  94. carla_stderr2("Failed to init plugin, possible reasons: %s", getLastError());
  95. return;
  96. }
  97. CARLA_SAFE_ASSERT_RETURN(pData->curPluginCount == 1,)
  98. fPlugin = pData->plugins[0].plugin;
  99. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  100. CARLA_SAFE_ASSERT_RETURN(fPlugin->isEnabled(),);
  101. fPorts.init(fPlugin);
  102. }
  103. ~CarlaEngineLV2Single()
  104. {
  105. if (fPlugin != nullptr && fIsActive)
  106. fPlugin->setActive(false, false, false);
  107. close();
  108. }
  109. bool hasPlugin() noexcept
  110. {
  111. return fPlugin != nullptr;
  112. }
  113. // -----------------------------------------------------------------------------------------------------------------
  114. // LV2 functions
  115. void lv2_connect_port(const uint32_t port, void* const dataLocation) noexcept
  116. {
  117. fPorts.connectPort(port, dataLocation);
  118. }
  119. void lv2_activate() noexcept
  120. {
  121. CARLA_SAFE_ASSERT_RETURN(! fIsActive,);
  122. fPlugin->setActive(true, false, false);
  123. fIsActive = true;
  124. }
  125. void lv2_deactivate() noexcept
  126. {
  127. CARLA_SAFE_ASSERT_RETURN(fIsActive,);
  128. fIsActive = false;
  129. fPlugin->setActive(false, false, false);
  130. }
  131. void lv2_run(const uint32_t frames)
  132. {
  133. fIsOffline = (fPorts.freewheel != nullptr && *fPorts.freewheel >= 0.5f);
  134. // Check for updated parameters
  135. float curValue;
  136. for (uint32_t i=0; i < fPorts.numParams; ++i)
  137. {
  138. if (fPorts.paramsOut[i])
  139. continue;
  140. CARLA_SAFE_ASSERT_CONTINUE(fPorts.paramsPtr[i] != nullptr)
  141. curValue = *fPorts.paramsPtr[i];
  142. if (carla_isEqual(fPorts.paramsLast[i], curValue))
  143. continue;
  144. fPorts.paramsLast[i] = curValue;
  145. fPlugin->setParameterValue(i, curValue, false, false, false);
  146. }
  147. if (frames == 0)
  148. return fPorts.updateOutputs();
  149. if (fPlugin->tryLock(fIsOffline))
  150. {
  151. fPlugin->initBuffers();
  152. fPlugin->process(fPorts.audioIns, fPorts.audioOuts, nullptr, nullptr, frames);
  153. fPlugin->unlock();
  154. }
  155. else
  156. {
  157. for (uint32_t i=0; i<fPorts.numAudioOuts; ++i)
  158. carla_zeroFloats(fPorts.audioOuts[i], frames);
  159. }
  160. fPorts.updateOutputs();
  161. }
  162. // -----------------------------------------------------------------------------------------------------------------
  163. void lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  164. LV2UI_Widget* widget, const LV2_Feature* const* features)
  165. {
  166. fUI.writeFunction = writeFunction;
  167. fUI.controller = controller;
  168. const LV2_URID_Map* uridMap = nullptr;
  169. // -------------------------------------------------------------------------------------------------------------
  170. // see if the host supports external-ui, get uridMap
  171. for (int i=0; features[i] != nullptr; ++i)
  172. {
  173. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  174. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  175. {
  176. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  177. }
  178. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  179. {
  180. uridMap = (const LV2_URID_Map*)features[i]->data;
  181. }
  182. }
  183. if (fUI.host != nullptr)
  184. {
  185. fUI.name = fUI.host->plugin_human_id;
  186. *widget = (LV2_External_UI_Widget*)this;
  187. return;
  188. }
  189. // -------------------------------------------------------------------------------------------------------------
  190. // no external-ui support, use showInterface
  191. for (int i=0; features[i] != nullptr; ++i)
  192. {
  193. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  194. {
  195. const LV2_Options_Option* const options((const LV2_Options_Option*)features[i]->data);
  196. for (int j=0; options[j].key != 0; ++j)
  197. {
  198. if (options[j].key == uridMap->map(uridMap->handle, LV2_UI__windowTitle))
  199. {
  200. fUI.name = (const char*)options[j].value;
  201. break;
  202. }
  203. }
  204. break;
  205. }
  206. }
  207. if (fUI.name.isEmpty())
  208. fUI.name = fPlugin->getName();
  209. *widget = nullptr;
  210. }
  211. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer) const
  212. {
  213. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  214. return;
  215. if (portIndex >= fPorts.indexOffset || ! fUI.visible)
  216. return;
  217. const float value(*(const float*)buffer);
  218. fPlugin->uiParameterChange(portIndex-fPorts.indexOffset, value);
  219. }
  220. void lv2ui_cleanup()
  221. {
  222. if (fUI.visible)
  223. handleUiHide();
  224. fUI.writeFunction = nullptr;
  225. fUI.controller = nullptr;
  226. fUI.host = nullptr;
  227. }
  228. // -----------------------------------------------------------------------------------------------------------------
  229. int lv2ui_idle() const
  230. {
  231. if (! fUI.visible)
  232. return 1;
  233. handleUiRun();
  234. return 0;
  235. }
  236. int lv2ui_show()
  237. {
  238. handleUiShow();
  239. return 0;
  240. }
  241. int lv2ui_hide()
  242. {
  243. handleUiHide();
  244. return 0;
  245. }
  246. protected:
  247. // -----------------------------------------------------------------------------------------------------------------
  248. // CarlaEngine virtual calls
  249. bool init(const char* const clientName) override
  250. {
  251. carla_stdout("CarlaEngineNative::init(\"%s\")", clientName);
  252. if (! pData->init(clientName))
  253. {
  254. close();
  255. setLastError("Failed to init internal data");
  256. return false;
  257. }
  258. return true;
  259. }
  260. bool isRunning() const noexcept override
  261. {
  262. return fIsActive;
  263. }
  264. bool isOffline() const noexcept override
  265. {
  266. return fIsOffline;
  267. }
  268. bool usesConstantBufferSize() const noexcept override
  269. {
  270. return true;
  271. }
  272. EngineType getType() const noexcept override
  273. {
  274. return kEngineTypePlugin;
  275. }
  276. const char* getCurrentDriverName() const noexcept override
  277. {
  278. return "LV2 Plugin";
  279. }
  280. void engineCallback(const EngineCallbackOpcode action, const uint pluginId, const int value1, const int value2, const float value3, const char* const valueStr)
  281. {
  282. switch (action)
  283. {
  284. case ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED:
  285. CARLA_SAFE_ASSERT_RETURN(value1 >= 0,);
  286. if (fUI.writeFunction != nullptr && fUI.controller != nullptr && fUI.visible)
  287. fUI.writeFunction(fUI.controller,
  288. static_cast<uint32_t>(value1)+fPorts.indexOffset,
  289. sizeof(float), 0, &value3);
  290. break;
  291. case ENGINE_CALLBACK_UI_STATE_CHANGED:
  292. fUI.visible = value1 == 1;
  293. if (fUI.host != nullptr)
  294. fUI.host->ui_closed(fUI.controller);
  295. break;
  296. case ENGINE_CALLBACK_IDLE:
  297. break;
  298. default:
  299. carla_stdout("engineCallback(%i:%s, %u, %i, %i, %f, %s)", action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  300. break;
  301. }
  302. }
  303. // -----------------------------------------------------------------------------------------------------------------
  304. void handleUiRun() const
  305. {
  306. try {
  307. fPlugin->uiIdle();
  308. } CARLA_SAFE_EXCEPTION("fPlugin->uiIdle()")
  309. }
  310. void handleUiShow()
  311. {
  312. fPlugin->showCustomUI(true);
  313. fUI.visible = true;
  314. }
  315. void handleUiHide()
  316. {
  317. fUI.visible = false;
  318. fPlugin->showCustomUI(false);
  319. }
  320. // -----------------------------------------------------------------------------------------------------------------
  321. private:
  322. CarlaPlugin* fPlugin;
  323. // Lv2 host data
  324. bool fIsActive;
  325. bool fIsOffline;
  326. struct Ports {
  327. uint32_t numAudioIns;
  328. uint32_t numAudioOuts;
  329. uint32_t numParams;
  330. const float** audioIns;
  331. /* */ float** audioOuts;
  332. float* freewheel;
  333. float* paramsLast;
  334. float** paramsPtr;
  335. bool* paramsOut;
  336. uint32_t indexOffset;
  337. const CarlaPlugin* plugin;
  338. Ports()
  339. : numAudioIns(0),
  340. numAudioOuts(0),
  341. numParams(0),
  342. audioIns(nullptr),
  343. audioOuts(nullptr),
  344. freewheel(nullptr),
  345. paramsLast(nullptr),
  346. paramsPtr(nullptr),
  347. paramsOut(nullptr),
  348. indexOffset(1),
  349. plugin(nullptr) {}
  350. ~Ports()
  351. {
  352. if (audioIns != nullptr)
  353. {
  354. delete[] audioIns;
  355. audioIns = nullptr;
  356. }
  357. if (audioOuts != nullptr)
  358. {
  359. delete[] audioOuts;
  360. audioOuts = nullptr;
  361. }
  362. if (paramsLast != nullptr)
  363. {
  364. delete[] paramsLast;
  365. paramsLast = nullptr;
  366. }
  367. if (paramsPtr != nullptr)
  368. {
  369. delete[] paramsPtr;
  370. paramsPtr = nullptr;
  371. }
  372. if (paramsOut != nullptr)
  373. {
  374. delete[] paramsOut;
  375. paramsOut = nullptr;
  376. }
  377. }
  378. void init(const CarlaPlugin* const pl)
  379. {
  380. plugin = pl;
  381. if ((numAudioIns = plugin->getAudioInCount()) > 0)
  382. {
  383. audioIns = new const float*[numAudioIns];
  384. for (uint32_t i=0; i < numAudioIns; ++i)
  385. audioIns[i] = nullptr;
  386. }
  387. if ((numAudioOuts = plugin->getAudioOutCount()) > 0)
  388. {
  389. audioOuts = new float*[numAudioOuts];
  390. for (uint32_t i=0; i < numAudioOuts; ++i)
  391. audioOuts[i] = nullptr;
  392. }
  393. if ((numParams = plugin->getParameterCount()) > 0)
  394. {
  395. paramsLast = new float[numParams];
  396. paramsPtr = new float*[numParams];
  397. paramsOut = new bool[numParams];
  398. for (uint32_t i=0; i < numParams; ++i)
  399. {
  400. paramsLast[i] = plugin->getParameterValue(i);
  401. paramsPtr [i] = nullptr;
  402. paramsOut [i] = plugin->isParameterOutput(i);
  403. }
  404. }
  405. indexOffset = numAudioIns + numAudioOuts + 1;
  406. }
  407. void connectPort(const uint32_t port, void* const dataLocation) noexcept
  408. {
  409. uint32_t index = 0;
  410. for (uint32_t i=0; i < numAudioIns; ++i)
  411. {
  412. if (port == index++)
  413. {
  414. audioIns[i] = (float*)dataLocation;
  415. return;
  416. }
  417. }
  418. for (uint32_t i=0; i < numAudioOuts; ++i)
  419. {
  420. if (port == index++)
  421. {
  422. audioOuts[i] = (float*)dataLocation;
  423. return;
  424. }
  425. }
  426. if (port == index++)
  427. {
  428. freewheel = (float*)dataLocation;
  429. return;
  430. }
  431. for (uint32_t i=0; i < numParams; ++i)
  432. {
  433. if (port == index++)
  434. {
  435. paramsPtr[i] = (float*)dataLocation;
  436. return;
  437. }
  438. }
  439. }
  440. void updateOutputs() noexcept
  441. {
  442. for (uint32_t i=0; i < numParams; ++i)
  443. {
  444. if (! paramsOut[i])
  445. continue;
  446. paramsLast[i] = plugin->getParameterValue(i);
  447. if (paramsPtr[i] != nullptr)
  448. *paramsPtr[i] = paramsLast[i];
  449. }
  450. }
  451. CARLA_DECLARE_NON_COPY_STRUCT(Ports);
  452. } fPorts;
  453. struct UI {
  454. LV2UI_Write_Function writeFunction;
  455. LV2UI_Controller controller;
  456. const LV2_External_UI_Host* host;
  457. CarlaString name;
  458. bool visible;
  459. UI()
  460. : writeFunction(nullptr),
  461. controller(nullptr),
  462. host(nullptr),
  463. name(),
  464. visible(false) {}
  465. CARLA_DECLARE_NON_COPY_STRUCT(UI)
  466. } fUI;
  467. // -------------------------------------------------------------------
  468. #define handlePtr ((CarlaEngineLV2Single*)handle)
  469. static void extui_run(LV2_External_UI_Widget_Compat* handle)
  470. {
  471. handlePtr->handleUiRun();
  472. }
  473. static void extui_show(LV2_External_UI_Widget_Compat* handle)
  474. {
  475. handlePtr->handleUiShow();
  476. }
  477. static void extui_hide(LV2_External_UI_Widget_Compat* handle)
  478. {
  479. handlePtr->handleUiHide();
  480. }
  481. static void _engine_callback(void* handle, EngineCallbackOpcode action, uint pluginId, int value1, int value2, float value3, const char* valueStr)
  482. {
  483. handlePtr->engineCallback(action, pluginId, value1, value2, value3, valueStr);
  484. }
  485. #undef handlePtr
  486. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineLV2Single)
  487. };
  488. CARLA_BACKEND_END_NAMESPACE
  489. CARLA_BACKEND_USE_NAMESPACE
  490. // ---------------------------------------------------------------------------------------------------------------------
  491. // LV2 DSP functions
  492. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  493. {
  494. carla_stdout("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  495. const LV2_Options_Option* options = nullptr;
  496. const LV2_URID_Map* uridMap = nullptr;
  497. const LV2_URID_Unmap* uridUnmap = nullptr;
  498. for (int i=0; features[i] != nullptr; ++i)
  499. {
  500. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  501. options = (const LV2_Options_Option*)features[i]->data;
  502. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  503. uridMap = (const LV2_URID_Map*)features[i]->data;
  504. else if (std::strcmp(features[i]->URI, LV2_URID__unmap) == 0)
  505. uridUnmap = (const LV2_URID_Unmap*)features[i]->data;
  506. }
  507. if (options == nullptr || uridMap == nullptr)
  508. {
  509. carla_stderr("Host doesn't provide option or urid-map features");
  510. return nullptr;
  511. }
  512. uint32_t bufferSize = 0;
  513. for (int i=0; options[i].key != 0; ++i)
  514. {
  515. if (uridUnmap != nullptr) {
  516. carla_stdout("Host option %i:\"%s\"", i, uridUnmap->unmap(uridUnmap->handle, options[i].key));
  517. }
  518. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__nominalBlockLength))
  519. {
  520. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  521. {
  522. const int value(*(const int*)options[i].value);
  523. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  524. bufferSize = static_cast<uint32_t>(value);
  525. }
  526. else
  527. {
  528. carla_stderr("Host provides nominalBlockLength but has wrong value type");
  529. }
  530. break;
  531. }
  532. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  533. {
  534. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  535. {
  536. const int value(*(const int*)options[i].value);
  537. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  538. bufferSize = static_cast<uint32_t>(value);
  539. }
  540. else
  541. {
  542. carla_stderr("Host provides maxBlockLength but has wrong value type");
  543. }
  544. // no break, continue in case host supports nominalBlockLength
  545. }
  546. }
  547. if (bufferSize == 0)
  548. {
  549. carla_stderr("Host doesn't provide bufferSize feature");
  550. return nullptr;
  551. }
  552. CarlaEngineLV2Single* const instance(new CarlaEngineLV2Single(bufferSize, sampleRate, bundlePath, uridMap));
  553. if (instance->hasPlugin())
  554. return (LV2_Handle)instance;
  555. delete instance;
  556. return nullptr;
  557. }
  558. #define instancePtr ((CarlaEngineLV2Single*)instance)
  559. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  560. {
  561. instancePtr->lv2_connect_port(port, dataLocation);
  562. }
  563. static void lv2_activate(LV2_Handle instance)
  564. {
  565. carla_stdout("lv2_activate(%p)", instance);
  566. instancePtr->lv2_activate();
  567. }
  568. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  569. {
  570. instancePtr->lv2_run(sampleCount);
  571. }
  572. static void lv2_deactivate(LV2_Handle instance)
  573. {
  574. carla_stdout("lv2_deactivate(%p)", instance);
  575. instancePtr->lv2_deactivate();
  576. }
  577. static void lv2_cleanup(LV2_Handle instance)
  578. {
  579. carla_stdout("lv2_cleanup(%p)", instance);
  580. delete instancePtr;
  581. }
  582. static const void* lv2_extension_data(const char* uri)
  583. {
  584. carla_stdout("lv2_extension_data(\"%s\")", uri);
  585. return nullptr;
  586. }
  587. #undef instancePtr
  588. // ---------------------------------------------------------------------------------------------------------------------
  589. // LV2 UI functions
  590. static LV2UI_Handle lv2ui_instantiate(const LV2UI_Descriptor*, const char*, const char*,
  591. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  592. LV2UI_Widget* widget, const LV2_Feature* const* features)
  593. {
  594. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  595. CarlaEngineLV2Single* engine = nullptr;
  596. for (int i=0; features[i] != nullptr; ++i)
  597. {
  598. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  599. {
  600. engine = (CarlaEngineLV2Single*)features[i]->data;
  601. break;
  602. }
  603. }
  604. if (engine == nullptr)
  605. {
  606. carla_stderr("Host doesn't support instance-access, cannot show UI");
  607. return nullptr;
  608. }
  609. engine->lv2ui_instantiate(writeFunction, controller, widget, features);
  610. return (LV2UI_Handle)engine;
  611. }
  612. #define uiPtr ((CarlaEngineLV2Single*)ui)
  613. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  614. {
  615. carla_debug("lv2ui_port_event(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  616. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  617. }
  618. static void lv2ui_cleanup(LV2UI_Handle ui)
  619. {
  620. carla_debug("lv2ui_cleanup(%p)", ui);
  621. uiPtr->lv2ui_cleanup();
  622. }
  623. static int lv2ui_idle(LV2UI_Handle ui)
  624. {
  625. return uiPtr->lv2ui_idle();
  626. }
  627. static int lv2ui_show(LV2UI_Handle ui)
  628. {
  629. carla_debug("lv2ui_show(%p)", ui);
  630. return uiPtr->lv2ui_show();
  631. }
  632. static int lv2ui_hide(LV2UI_Handle ui)
  633. {
  634. carla_debug("lv2ui_hide(%p)", ui);
  635. return uiPtr->lv2ui_hide();
  636. }
  637. static const void* lv2ui_extension_data(const char* uri)
  638. {
  639. carla_stdout("lv2ui_extension_data(\"%s\")", uri);
  640. static const LV2UI_Idle_Interface uiidle = { lv2ui_idle };
  641. static const LV2UI_Show_Interface uishow = { lv2ui_show, lv2ui_hide };
  642. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  643. return &uiidle;
  644. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  645. return &uishow;
  646. return nullptr;
  647. }
  648. #undef uiPtr
  649. // ---------------------------------------------------------------------------------------------------------------------
  650. // Startup code
  651. CARLA_EXPORT
  652. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  653. {
  654. carla_stdout("lv2_descriptor(%i)", index);
  655. if (index != 0)
  656. return nullptr;
  657. static CarlaString ret;
  658. if (ret.isEmpty())
  659. {
  660. using namespace juce;
  661. const File file(File::getSpecialLocation(File::currentExecutableFile).withFileExtension("ttl"));
  662. ret = String("file://" + file.getFullPathName()).toRawUTF8();
  663. }
  664. static const LV2_Descriptor desc = {
  665. /* URI */ ret.buffer(),
  666. /* instantiate */ lv2_instantiate,
  667. /* connect_port */ lv2_connect_port,
  668. /* activate */ lv2_activate,
  669. /* run */ lv2_run,
  670. /* deactivate */ lv2_deactivate,
  671. /* cleanup */ lv2_cleanup,
  672. /* extension_data */ lv2_extension_data
  673. };
  674. return &desc;
  675. }
  676. CARLA_EXPORT
  677. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  678. {
  679. carla_stdout("lv2ui_descriptor(%i)", index);
  680. static CarlaString ret;
  681. if (ret.isEmpty())
  682. {
  683. using namespace juce;
  684. const File file(File::getSpecialLocation(File::currentExecutableFile).getSiblingFile("ext-ui"));
  685. ret = String("file://" + file.getFullPathName()).toRawUTF8();
  686. }
  687. static const LV2UI_Descriptor lv2UiExtDesc = {
  688. /* URI */ ret.buffer(),
  689. /* instantiate */ lv2ui_instantiate,
  690. /* cleanup */ lv2ui_cleanup,
  691. /* port_event */ lv2ui_port_event,
  692. /* extension_data */ lv2ui_extension_data
  693. };
  694. return (index == 0) ? &lv2UiExtDesc : nullptr;
  695. }
  696. // ---------------------------------------------------------------------------------------------------------------------