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 23KB

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