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