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.

CarlaBridgeSingleLV2.cpp 24KB

6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. * Carla LV2 Single Plugin
  3. * Copyright (C) 2017-2020 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. #ifdef USING_JUCE
  27. # if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  28. # pragma GCC diagnostic push
  29. # pragma GCC diagnostic ignored "-Wconversion"
  30. # pragma GCC diagnostic ignored "-Weffc++"
  31. # pragma GCC diagnostic ignored "-Wsign-conversion"
  32. # pragma GCC diagnostic ignored "-Wundef"
  33. # pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
  34. # endif
  35. # include "AppConfig.h"
  36. # include "juce_events/juce_events.h"
  37. # if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  38. # pragma GCC diagnostic pop
  39. # endif
  40. #endif
  41. #include "water/files/File.h"
  42. template<>
  43. void Lv2PluginBaseClass<CarlaBackend::EngineTimeInfo>::clearTimeData() noexcept
  44. {
  45. fLastPositionData.clear();
  46. fTimeInfo.clear();
  47. }
  48. // --------------------------------------------------------------------------------------------------------------------
  49. CARLA_BACKEND_START_NAMESPACE
  50. class CarlaEngineSingleLV2 : public CarlaEngine,
  51. public Lv2PluginBaseClass<EngineTimeInfo>
  52. {
  53. public:
  54. CarlaEngineSingleLV2(const double sampleRate,
  55. const char* const bundlePath,
  56. const LV2_Feature* const* const features)
  57. : Lv2PluginBaseClass<EngineTimeInfo>(sampleRate, features),
  58. fPlugin(nullptr),
  59. fUiName()
  60. #ifdef USING_JUCE
  61. , fJuceInitialiser()
  62. #endif
  63. {
  64. CARLA_SAFE_ASSERT_RETURN(pData->curPluginCount == 0,)
  65. CARLA_SAFE_ASSERT_RETURN(pData->plugins == nullptr,);
  66. if (! loadedInProperHost())
  67. return;
  68. // xxxxx
  69. CarlaString binaryDir(bundlePath);
  70. binaryDir += CARLA_OS_SEP_STR "bin" CARLA_OS_SEP_STR;
  71. CarlaString resourceDir(bundlePath);
  72. resourceDir += CARLA_OS_SEP_STR "res" CARLA_OS_SEP_STR;
  73. pData->bufferSize = fBufferSize;
  74. pData->sampleRate = sampleRate;
  75. pData->initTime(nullptr);
  76. pData->options.processMode = ENGINE_PROCESS_MODE_BRIDGE;
  77. pData->options.transportMode = ENGINE_TRANSPORT_MODE_PLUGIN;
  78. pData->options.forceStereo = false;
  79. pData->options.preferPluginBridges = false;
  80. pData->options.preferUiBridges = false;
  81. init("LV2-Export");
  82. if (pData->options.resourceDir != nullptr)
  83. delete[] pData->options.resourceDir;
  84. if (pData->options.binaryDir != nullptr)
  85. delete[] pData->options.binaryDir;
  86. pData->options.binaryDir = binaryDir.dup();
  87. pData->options.resourceDir = resourceDir.dup();
  88. setCallback(_engine_callback, this);
  89. using water::File;
  90. const File pluginFile(File::getSpecialLocation(File::currentExecutableFile).withFileExtension("xml"));
  91. if (! loadProject(pluginFile.getFullPathName().toRawUTF8(), true))
  92. {
  93. carla_stderr2("Failed to init plugin, possible reasons: %s", getLastError());
  94. return;
  95. }
  96. CARLA_SAFE_ASSERT_RETURN(pData->curPluginCount == 1,)
  97. fPlugin = pData->plugins[0].plugin;
  98. CARLA_SAFE_ASSERT_RETURN(fPlugin.get() != nullptr,);
  99. CARLA_SAFE_ASSERT_RETURN(fPlugin->isEnabled(),);
  100. fPorts.hasUI = false;
  101. fPorts.usesTime = true;
  102. fPorts.numAudioIns = fPlugin->getAudioInCount();
  103. fPorts.numAudioOuts = fPlugin->getAudioOutCount();
  104. fPorts.numCVIns = fPlugin->getCVInCount();
  105. fPorts.numCVOuts = fPlugin->getCVOutCount();
  106. fPorts.numMidiIns = fPlugin->getMidiInCount();
  107. fPorts.numMidiOuts = fPlugin->getMidiOutCount();
  108. fPorts.numParams = fPlugin->getParameterCount();
  109. fPorts.init();
  110. for (uint32_t i=0; i < fPorts.numParams; ++i)
  111. {
  112. fPorts.paramsLast[i] = fPlugin->getParameterValue(i);
  113. fPorts.paramsOut [i] = fPlugin->isParameterOutput(i);
  114. }
  115. }
  116. ~CarlaEngineSingleLV2()
  117. {
  118. if (fPlugin.get() != nullptr && fIsActive)
  119. fPlugin->setActive(false, false, false);
  120. fPlugin.reset();
  121. close();
  122. }
  123. bool hasPlugin() noexcept
  124. {
  125. return fPlugin.get() != nullptr;
  126. }
  127. // ----------------------------------------------------------------------------------------------------------------
  128. // LV2 functions
  129. void lv2_activate() noexcept
  130. {
  131. CARLA_SAFE_ASSERT_RETURN(! fIsActive,);
  132. resetTimeInfo();
  133. fPlugin->setActive(true, false, false);
  134. fIsActive = true;
  135. }
  136. void lv2_deactivate() noexcept
  137. {
  138. CARLA_SAFE_ASSERT_RETURN(fIsActive,);
  139. fIsActive = false;
  140. fPlugin->setActive(false, false, false);
  141. }
  142. void lv2_run(const uint32_t frames)
  143. {
  144. //const PendingRtEventsRunner prt(this, frames);
  145. if (! lv2_pre_run(frames))
  146. {
  147. updateParameterOutputs();
  148. return;
  149. }
  150. if (fPorts.numMidiIns > 0)
  151. {
  152. uint32_t engineEventIndex = 0;
  153. carla_zeroStructs(pData->events.in, kMaxEngineEventInternalCount);
  154. for (uint32_t i=0; i < fPorts.numMidiIns; ++i)
  155. {
  156. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[i], event)
  157. {
  158. if (event == nullptr)
  159. continue;
  160. if (event->body.type != fURIs.midiEvent)
  161. continue;
  162. if (event->body.size > 4)
  163. continue;
  164. if (event->time.frames >= frames)
  165. break;
  166. const uint8_t* const data((const uint8_t*)(event + 1));
  167. EngineEvent& engineEvent(pData->events.in[engineEventIndex++]);
  168. engineEvent.time = (uint32_t)event->time.frames;
  169. engineEvent.fillFromMidiData((uint8_t)event->body.size, data, (uint8_t)i);
  170. if (engineEventIndex >= kMaxEngineEventInternalCount)
  171. break;
  172. }
  173. }
  174. }
  175. if (fPorts.numMidiOuts > 0)
  176. {
  177. carla_zeroStructs(pData->events.out, kMaxEngineEventInternalCount);
  178. }
  179. if (fPlugin->tryLock(fIsOffline))
  180. {
  181. fPlugin->initBuffers();
  182. fPlugin->process(fPorts.audioCVIns,
  183. fPorts.audioCVOuts,
  184. fPorts.audioCVIns + fPorts.numAudioIns,
  185. fPorts.audioCVOuts + fPorts.numAudioOuts,
  186. frames);
  187. fPlugin->unlock();
  188. if (fPorts.numMidiOuts > 0)
  189. {
  190. uint8_t port = 0;
  191. uint8_t size = 0;
  192. uint8_t mdata[3] = { 0, 0, 0 };
  193. uint8_t mdataTmp[EngineMidiEvent::kDataSize];
  194. const uint8_t* mdataPtr;
  195. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  196. {
  197. const EngineEvent& engineEvent(pData->events.out[i]);
  198. /**/ if (engineEvent.type == kEngineEventTypeNull)
  199. {
  200. break;
  201. }
  202. else if (engineEvent.type == kEngineEventTypeControl)
  203. {
  204. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  205. size = ctrlEvent.convertToMidiData(engineEvent.channel, mdata);
  206. mdataPtr = mdata;
  207. }
  208. else if (engineEvent.type == kEngineEventTypeMidi)
  209. {
  210. const EngineMidiEvent& midiEvent(engineEvent.midi);
  211. port = midiEvent.port;
  212. size = midiEvent.size;
  213. CARLA_SAFE_ASSERT_CONTINUE(size > 0);
  214. if (size > EngineMidiEvent::kDataSize)
  215. {
  216. CARLA_SAFE_ASSERT_CONTINUE(midiEvent.dataExt != nullptr);
  217. mdataPtr = midiEvent.dataExt;
  218. }
  219. else
  220. {
  221. // set first byte
  222. mdataTmp[0] = static_cast<uint8_t>(midiEvent.data[0] | (engineEvent.channel & MIDI_CHANNEL_BIT));
  223. // copy rest
  224. carla_copy<uint8_t>(mdataTmp+1, midiEvent.data+1, size-1U);
  225. // done
  226. mdataPtr = mdataTmp;
  227. }
  228. }
  229. else
  230. {
  231. continue;
  232. }
  233. if (size > 0 && ! writeMidiEvent(port, engineEvent.time, size, mdataPtr))
  234. break;
  235. }
  236. }
  237. }
  238. else
  239. {
  240. for (uint32_t i=0; i<fPorts.numAudioOuts; ++i)
  241. carla_zeroFloats(fPorts.audioCVOuts[i], frames);
  242. for (uint32_t i=0; i<fPorts.numCVOuts; ++i)
  243. carla_zeroFloats(fPorts.audioCVOuts[fPorts.numAudioOuts+i], frames);
  244. }
  245. lv2_post_run(frames);
  246. updateParameterOutputs();
  247. }
  248. // ----------------------------------------------------------------------------------------------------------------
  249. bool lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  250. LV2UI_Widget* widget, const LV2_Feature* const* features)
  251. {
  252. fUI.writeFunction = writeFunction;
  253. fUI.controller = controller;
  254. fUI.host = nullptr;
  255. fUiName.clear();
  256. const LV2_URID_Map* uridMap = nullptr;
  257. // ------------------------------------------------------------------------------------------------------------
  258. // see if the host supports external-ui, get uridMap
  259. for (int i=0; features[i] != nullptr; ++i)
  260. {
  261. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  262. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  263. {
  264. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  265. }
  266. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  267. {
  268. uridMap = (const LV2_URID_Map*)features[i]->data;
  269. }
  270. }
  271. if (fUI.host != nullptr)
  272. {
  273. fUiName = fUI.host->plugin_human_id;
  274. *widget = (LV2_External_UI_Widget_Compat*)this;
  275. return true;
  276. }
  277. // ------------------------------------------------------------------------------------------------------------
  278. // no external-ui support, use showInterface
  279. for (int i=0; features[i] != nullptr; ++i)
  280. {
  281. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  282. {
  283. const LV2_Options_Option* const options((const LV2_Options_Option*)features[i]->data);
  284. for (int j=0; options[j].key != 0; ++j)
  285. {
  286. if (options[j].key == uridMap->map(uridMap->handle, LV2_UI__windowTitle))
  287. {
  288. fUiName = (const char*)options[j].value;
  289. break;
  290. }
  291. }
  292. break;
  293. }
  294. }
  295. if (fUiName.isEmpty())
  296. fUiName = fPlugin->getName();
  297. *widget = nullptr;
  298. return true;
  299. }
  300. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer) const
  301. {
  302. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  303. return;
  304. if (portIndex >= fPorts.indexOffset || ! fUI.isVisible)
  305. return;
  306. const float value(*(const float*)buffer);
  307. fPlugin->uiParameterChange(portIndex-fPorts.indexOffset, value);
  308. }
  309. protected:
  310. // ----------------------------------------------------------------------------------------------------------------
  311. // CarlaEngine virtual calls
  312. bool init(const char* const clientName) override
  313. {
  314. carla_stdout("CarlaEngineNative::init(\"%s\")", clientName);
  315. if (! pData->init(clientName))
  316. {
  317. close();
  318. setLastError("Failed to init internal data");
  319. return false;
  320. }
  321. return true;
  322. }
  323. bool isRunning() const noexcept override
  324. {
  325. return fIsActive;
  326. }
  327. bool isOffline() const noexcept override
  328. {
  329. return fIsOffline;
  330. }
  331. bool usesConstantBufferSize() const noexcept override
  332. {
  333. return false;
  334. }
  335. EngineType getType() const noexcept override
  336. {
  337. return kEngineTypePlugin;
  338. }
  339. const char* getCurrentDriverName() const noexcept override
  340. {
  341. return "LV2 Plugin";
  342. }
  343. void engineCallback(const EngineCallbackOpcode action, const uint pluginId,
  344. const int value1, const int value2, const int value3,
  345. const float valuef, const char* const valueStr)
  346. {
  347. switch (action)
  348. {
  349. case ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED:
  350. if (value1 == PARAMETER_ACTIVE)
  351. return;
  352. CARLA_SAFE_ASSERT_RETURN(value1 >= 0,);
  353. if (fUI.writeFunction != nullptr && fUI.controller != nullptr && fUI.isVisible)
  354. {
  355. fUI.writeFunction(fUI.controller,
  356. static_cast<uint32_t>(value1)+fPorts.indexOffset,
  357. sizeof(float), 0, &valuef);
  358. }
  359. break;
  360. case ENGINE_CALLBACK_UI_STATE_CHANGED:
  361. fUI.isVisible = (value1 == 1);
  362. if (fUI.host != nullptr)
  363. fUI.host->ui_closed(fUI.controller);
  364. break;
  365. case ENGINE_CALLBACK_IDLE:
  366. break;
  367. default:
  368. carla_stdout("engineCallback(%i:%s, %u, %i, %i, %f, %s)",
  369. action, EngineCallbackOpcode2Str(action), pluginId,
  370. value1, value2, value3,
  371. static_cast<double>(valuef), valueStr);
  372. break;
  373. }
  374. }
  375. // ----------------------------------------------------------------------------------------------------------------
  376. void handleUiRun() const override
  377. {
  378. try {
  379. fPlugin->uiIdle();
  380. } CARLA_SAFE_EXCEPTION("fPlugin->uiIdle()")
  381. }
  382. void handleUiShow() override
  383. {
  384. fPlugin->showCustomUI(true);
  385. fUI.isVisible = true;
  386. }
  387. void handleUiHide() override
  388. {
  389. fUI.isVisible = false;
  390. fPlugin->showCustomUI(false);
  391. }
  392. // ----------------------------------------------------------------------------------------------------------------
  393. void handleParameterValueChanged(const uint32_t index, const float value) override
  394. {
  395. fPlugin->setParameterValue(index, value, false, false, false);
  396. }
  397. void handleBufferSizeChanged(const uint32_t bufferSize) override
  398. {
  399. CarlaEngine::bufferSizeChanged(bufferSize);
  400. }
  401. void handleSampleRateChanged(const double sampleRate) override
  402. {
  403. CarlaEngine::sampleRateChanged(sampleRate);
  404. }
  405. // ----------------------------------------------------------------------------------------------------------------
  406. private:
  407. CarlaPluginPtr fPlugin;
  408. CarlaString fUiName;
  409. #ifdef USING_JUCE
  410. juce::SharedResourcePointer<juce::ScopedJuceInitialiser_GUI> fJuceInitialiser;
  411. #endif
  412. void updateParameterOutputs() noexcept
  413. {
  414. float value;
  415. for (uint32_t i=0; i < fPorts.numParams; ++i)
  416. {
  417. if (! fPorts.paramsOut[i])
  418. continue;
  419. fPorts.paramsLast[i] = value = fPlugin->getParameterValue(i);
  420. if (fPorts.paramsPtr[i] != nullptr)
  421. *fPorts.paramsPtr[i] = value;
  422. }
  423. }
  424. bool writeMidiEvent(const uint8_t port, const uint32_t time, const uint8_t midiSize, const uint8_t* midiData)
  425. {
  426. CARLA_SAFE_ASSERT_RETURN(fPorts.numMidiOuts > 0, false);
  427. CARLA_SAFE_ASSERT_RETURN(port < fPorts.numMidiOuts, false);
  428. CARLA_SAFE_ASSERT_RETURN(midiData != nullptr, false);
  429. CARLA_SAFE_ASSERT_RETURN(midiSize > 0, false);
  430. LV2_Atom_Sequence* const seq(fPorts.eventsOut[port]);
  431. CARLA_SAFE_ASSERT_RETURN(seq != nullptr, false);
  432. Ports::EventsOutData& mData(fPorts.eventsOutData[port]);
  433. if (sizeof(LV2_Atom_Event) + midiSize > mData.capacity - mData.offset)
  434. return false;
  435. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, seq) + mData.offset);
  436. aev->time.frames = time;
  437. aev->body.size = midiSize;
  438. aev->body.type = fURIs.midiEvent;
  439. std::memcpy(LV2_ATOM_BODY(&aev->body), midiData, midiSize);
  440. const uint32_t size = lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom_Event) + midiSize));
  441. mData.offset += size;
  442. seq->atom.size += size;
  443. return true;
  444. }
  445. // -------------------------------------------------------------------
  446. #define handlePtr ((CarlaEngineSingleLV2*)handle)
  447. static void _engine_callback(void* handle, EngineCallbackOpcode action, uint pluginId,
  448. int value1, int value2, int value3,
  449. float valuef, const char* valueStr)
  450. {
  451. handlePtr->engineCallback(action, pluginId, value1, value2, value3, valuef, valueStr);
  452. }
  453. #undef handlePtr
  454. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineSingleLV2)
  455. };
  456. CARLA_BACKEND_END_NAMESPACE
  457. using CarlaBackend::CarlaEngineSingleLV2;
  458. // --------------------------------------------------------------------------------------------------------------------
  459. // LV2 DSP functions
  460. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  461. {
  462. carla_stdout("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  463. CarlaEngineSingleLV2* const instance(new CarlaEngineSingleLV2(sampleRate, bundlePath, features));
  464. if (instance->hasPlugin())
  465. return (LV2_Handle)instance;
  466. delete instance;
  467. return nullptr;
  468. }
  469. #define instancePtr ((CarlaEngineSingleLV2*)instance)
  470. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  471. {
  472. instancePtr->lv2_connect_port(port, dataLocation);
  473. }
  474. static void lv2_activate(LV2_Handle instance)
  475. {
  476. carla_debug("lv2_activate(%p)", instance);
  477. instancePtr->lv2_activate();
  478. }
  479. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  480. {
  481. instancePtr->lv2_run(sampleCount);
  482. }
  483. static void lv2_deactivate(LV2_Handle instance)
  484. {
  485. carla_debug("lv2_deactivate(%p)", instance);
  486. instancePtr->lv2_deactivate();
  487. }
  488. static void lv2_cleanup(LV2_Handle instance)
  489. {
  490. carla_debug("lv2_cleanup(%p)", instance);
  491. delete instancePtr;
  492. }
  493. static const void* lv2_extension_data(const char* uri)
  494. {
  495. carla_debug("lv2_extension_data(\"%s\")", uri);
  496. return nullptr;
  497. // unused
  498. (void)uri;
  499. }
  500. #undef instancePtr
  501. // --------------------------------------------------------------------------------------------------------------------
  502. // LV2 UI functions
  503. static LV2UI_Handle lv2ui_instantiate(const LV2UI_Descriptor*, const char*, const char*,
  504. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  505. LV2UI_Widget* widget, const LV2_Feature* const* features)
  506. {
  507. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  508. CarlaEngineSingleLV2* engine = nullptr;
  509. for (int i=0; features[i] != nullptr; ++i)
  510. {
  511. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  512. {
  513. engine = (CarlaEngineSingleLV2*)features[i]->data;
  514. break;
  515. }
  516. }
  517. if (engine == nullptr)
  518. {
  519. carla_stderr("Host doesn't support instance-access, cannot show UI");
  520. return nullptr;
  521. }
  522. if (! engine->lv2ui_instantiate(writeFunction, controller, widget, features))
  523. return nullptr;
  524. return (LV2UI_Handle)engine;
  525. }
  526. #define uiPtr ((CarlaEngineSingleLV2*)ui)
  527. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  528. {
  529. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  530. }
  531. static void lv2ui_cleanup(LV2UI_Handle ui)
  532. {
  533. carla_debug("lv2ui_cleanup(%p)", ui);
  534. uiPtr->lv2ui_cleanup();
  535. }
  536. static int lv2ui_idle(LV2UI_Handle ui)
  537. {
  538. return uiPtr->lv2ui_idle();
  539. }
  540. static int lv2ui_show(LV2UI_Handle ui)
  541. {
  542. carla_debug("lv2ui_show(%p)", ui);
  543. return uiPtr->lv2ui_show();
  544. }
  545. static int lv2ui_hide(LV2UI_Handle ui)
  546. {
  547. carla_debug("lv2ui_hide(%p)", ui);
  548. return uiPtr->lv2ui_hide();
  549. }
  550. static const void* lv2ui_extension_data(const char* uri)
  551. {
  552. carla_debug("lv2ui_extension_data(\"%s\")", uri);
  553. static const LV2UI_Idle_Interface uiidle = { lv2ui_idle };
  554. static const LV2UI_Show_Interface uishow = { lv2ui_show, lv2ui_hide };
  555. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  556. return &uiidle;
  557. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  558. return &uishow;
  559. return nullptr;
  560. }
  561. #undef uiPtr
  562. // --------------------------------------------------------------------------------------------------------------------
  563. // Startup code
  564. CARLA_EXPORT
  565. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  566. {
  567. carla_debug("lv2_descriptor(%i)", index);
  568. if (index != 0)
  569. return nullptr;
  570. static CarlaString ret;
  571. if (ret.isEmpty())
  572. {
  573. using namespace water;
  574. const File file(File::getSpecialLocation(File::currentExecutableFile).withFileExtension("ttl"));
  575. #ifdef CARLA_OS_WIN
  576. ret = String("file:///" + file.getFullPathName()).toRawUTF8();
  577. ret.replace('\\','/');
  578. #else
  579. ret = String("file://" + file.getFullPathName()).toRawUTF8();
  580. #endif
  581. }
  582. carla_stdout("lv2_descriptor(%i) has URI '%s'", index, ret.buffer());
  583. static const LV2_Descriptor desc = {
  584. /* URI */ ret.buffer(),
  585. /* instantiate */ lv2_instantiate,
  586. /* connect_port */ lv2_connect_port,
  587. /* activate */ lv2_activate,
  588. /* run */ lv2_run,
  589. /* deactivate */ lv2_deactivate,
  590. /* cleanup */ lv2_cleanup,
  591. /* extension_data */ lv2_extension_data
  592. };
  593. return &desc;
  594. }
  595. CARLA_EXPORT
  596. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  597. {
  598. carla_debug("lv2ui_descriptor(%i)", index);
  599. static CarlaString ret;
  600. {
  601. using namespace water;
  602. const File file(File::getSpecialLocation(File::currentExecutableFile).getSiblingFile("ext-ui"));
  603. #ifdef CARLA_OS_WIN
  604. ret = String("file:///" + file.getFullPathName()).toRawUTF8();
  605. ret.replace('\\','/');
  606. #else
  607. ret = String("file://" + file.getFullPathName()).toRawUTF8();
  608. #endif
  609. }
  610. carla_stdout("lv2ui_descriptor(%i) has URI '%s'", index, ret.buffer());
  611. static const LV2UI_Descriptor lv2UiExtDesc = {
  612. /* URI */ ret.buffer(),
  613. /* instantiate */ lv2ui_instantiate,
  614. /* cleanup */ lv2ui_cleanup,
  615. /* port_event */ lv2ui_port_event,
  616. /* extension_data */ lv2ui_extension_data
  617. };
  618. return (index == 0) ? &lv2UiExtDesc : nullptr;
  619. }
  620. // --------------------------------------------------------------------------------------------------------------------