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.

1455 lines
45KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2014 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. #define CARLA_NATIVE_PLUGIN_LV2
  18. #include "carla-native-base.cpp"
  19. #include "CarlaLv2Utils.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #include "CarlaString.hpp"
  22. #include "juce_audio_basics.h"
  23. using juce::FloatVectorOperations;
  24. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  25. # include "juce_gui_basics.h"
  26. using juce::ScopedJuceInitialiser_GUI;
  27. using juce::SharedResourcePointer;
  28. #endif
  29. // -----------------------------------------------------------------------
  30. // -Weffc++ compat ext widget
  31. extern "C" {
  32. typedef struct _LV2_External_UI_Widget_Compat {
  33. void (*run )(struct _LV2_External_UI_Widget_Compat*);
  34. void (*show)(struct _LV2_External_UI_Widget_Compat*);
  35. void (*hide)(struct _LV2_External_UI_Widget_Compat*);
  36. _LV2_External_UI_Widget_Compat()
  37. : run(nullptr), show(nullptr), hide(nullptr) {}
  38. } LV2_External_UI_Widget_Compat;
  39. }
  40. // -----------------------------------------------------------------------
  41. // LV2 descriptor functions
  42. #if defined(__clang__)
  43. # pragma clang diagnostic push
  44. # pragma clang diagnostic ignored "-Weffc++"
  45. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  46. # pragma GCC diagnostic push
  47. # pragma GCC diagnostic ignored "-Weffc++"
  48. #endif
  49. class NativePlugin : 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. static const uint32_t kMaxMidiEvents = 512;
  58. NativePlugin(const NativePluginDescriptor* const desc, const double sampleRate, const char* const bundlePath, const LV2_Feature* const* features)
  59. : fHandle(nullptr),
  60. fHost(),
  61. fDescriptor(desc),
  62. #ifdef CARLA_PROPER_CPP11_SUPPORT
  63. fProgramDesc({0, 0, nullptr}),
  64. #endif
  65. fMidiEventCount(0),
  66. fTimeInfo(),
  67. fIsProcessing(false),
  68. fBufferSize(0),
  69. fSampleRate(sampleRate),
  70. fUridMap(nullptr),
  71. fURIs(),
  72. fUI(),
  73. fPorts(),
  74. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  75. sJuceGUI(),
  76. #endif
  77. leakDetector_NativePlugin()
  78. {
  79. run = extui_run;
  80. show = extui_show;
  81. hide = extui_hide;
  82. CarlaString resourceDir(bundlePath);
  83. #ifdef CARLA_OS_WIN
  84. resourceDir += "\\resources\\";
  85. #else
  86. resourceDir += "/resources/";
  87. #endif
  88. fHost.handle = this;
  89. fHost.resourceDir = resourceDir.dup();
  90. fHost.uiName = nullptr;
  91. fHost.uiParentId = 0;
  92. fHost.get_buffer_size = host_get_buffer_size;
  93. fHost.get_sample_rate = host_get_sample_rate;
  94. fHost.is_offline = host_is_offline;
  95. fHost.get_time_info = host_get_time_info;
  96. fHost.write_midi_event = host_write_midi_event;
  97. fHost.ui_parameter_changed = host_ui_parameter_changed;
  98. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  99. fHost.ui_closed = host_ui_closed;
  100. fHost.ui_open_file = host_ui_open_file;
  101. fHost.ui_save_file = host_ui_save_file;
  102. fHost.dispatcher = host_dispatcher;
  103. const LV2_Options_Option* options = nullptr;
  104. const LV2_URID_Map* uridMap = nullptr;
  105. const LV2_URID_Unmap* uridUnmap = nullptr;
  106. for (int i=0; features[i] != nullptr; ++i)
  107. {
  108. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  109. options = (const LV2_Options_Option*)features[i]->data;
  110. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  111. uridMap = (const LV2_URID_Map*)features[i]->data;
  112. else if (std::strcmp(features[i]->URI, LV2_URID__unmap) == 0)
  113. uridUnmap = (const LV2_URID_Unmap*)features[i]->data;
  114. }
  115. if (options == nullptr || uridMap == nullptr)
  116. {
  117. carla_stderr("Host doesn't provides option or urid-map features");
  118. return;
  119. }
  120. for (int i=0; options[i].key != 0; ++i)
  121. {
  122. if (uridUnmap != nullptr)
  123. {
  124. carla_debug("Host option %i:\"%s\"", i, uridUnmap->unmap(uridUnmap->handle, options[i].key));
  125. }
  126. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  127. {
  128. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  129. {
  130. const int value(*(const int*)options[i].value);
  131. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  132. fBufferSize = static_cast<uint32_t>(value);
  133. }
  134. else
  135. carla_stderr("Host provides maxBlockLength but has wrong value type");
  136. break;
  137. }
  138. }
  139. fUridMap = uridMap;
  140. if (fDescriptor->midiIns > 0)
  141. fUI.portOffset += desc->midiIns;
  142. else if (fDescriptor->hints & PLUGIN_USES_TIME)
  143. fUI.portOffset += 1;
  144. fUI.portOffset += desc->midiOuts;
  145. fUI.portOffset += 1; // freewheel
  146. fUI.portOffset += desc->audioIns;
  147. fUI.portOffset += desc->audioOuts;
  148. }
  149. ~NativePlugin()
  150. {
  151. CARLA_ASSERT(fHandle == nullptr);
  152. if (fHost.resourceDir != nullptr)
  153. {
  154. delete[] fHost.resourceDir;
  155. fHost.resourceDir = nullptr;
  156. }
  157. }
  158. bool init()
  159. {
  160. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  161. {
  162. carla_stderr("Plugin is missing something...");
  163. return false;
  164. }
  165. if (fBufferSize == 0)
  166. {
  167. carla_stderr("Host is missing bufferSize feature");
  168. //return false;
  169. // as testing, continue for now
  170. fBufferSize = 1024;
  171. }
  172. fHandle = fDescriptor->instantiate(&fHost);
  173. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  174. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kMaxMidiEvents*2);
  175. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  176. fPorts.init(fDescriptor, fHandle);
  177. fURIs.map(fUridMap);
  178. return true;
  179. }
  180. // -------------------------------------------------------------------
  181. // LV2 functions
  182. void lv2_connect_port(const uint32_t port, void* const dataLocation)
  183. {
  184. fPorts.connectPort(fDescriptor, port, dataLocation);
  185. }
  186. void lv2_activate()
  187. {
  188. if (fDescriptor->activate != nullptr)
  189. fDescriptor->activate(fHandle);
  190. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  191. }
  192. void lv2_deactivate()
  193. {
  194. if (fDescriptor->deactivate != nullptr)
  195. fDescriptor->deactivate(fHandle);
  196. }
  197. void lv2_cleanup()
  198. {
  199. if (fDescriptor->cleanup != nullptr)
  200. fDescriptor->cleanup(fHandle);
  201. fHandle = nullptr;
  202. }
  203. void lv2_run(const uint32_t frames)
  204. {
  205. if (frames == 0)
  206. {
  207. updateParameterOutputs();
  208. return;
  209. }
  210. // Check for updated parameters
  211. float curValue;
  212. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  213. {
  214. CARLA_SAFE_ASSERT_CONTINUE(fPorts.paramsPtr[i] != nullptr)
  215. curValue = *fPorts.paramsPtr[i];
  216. if ((! carla_compareFloats(fPorts.paramsLast[i], curValue)) && (fDescriptor->get_parameter_info(fHandle, i)->hints & PARAMETER_IS_OUTPUT) == 0)
  217. {
  218. fPorts.paramsLast[i] = curValue;
  219. fDescriptor->set_parameter_value(fHandle, i, curValue);
  220. }
  221. }
  222. if (fDescriptor->midiIns > 0 || (fDescriptor->hints & PLUGIN_USES_TIME) != 0)
  223. {
  224. fMidiEventCount = 0;
  225. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kMaxMidiEvents*2);
  226. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[0], iter)
  227. {
  228. const LV2_Atom_Event* const event((const LV2_Atom_Event*)iter);
  229. if (event == nullptr)
  230. continue;
  231. if (event->body.size > 4)
  232. continue;
  233. if (event->time.frames >= frames)
  234. break;
  235. if (event->body.type == fURIs.midiEvent)
  236. {
  237. if (fMidiEventCount >= kMaxMidiEvents*2)
  238. continue;
  239. const uint8_t* const data((const uint8_t*)(event + 1));
  240. fMidiEvents[fMidiEventCount].port = 0;
  241. fMidiEvents[fMidiEventCount].time = (uint32_t)event->time.frames;
  242. fMidiEvents[fMidiEventCount].size = (uint8_t)event->body.size;
  243. for (uint32_t i=0; i < event->body.size; ++i)
  244. fMidiEvents[fMidiEventCount].data[i] = data[i];
  245. fMidiEventCount += 1;
  246. continue;
  247. }
  248. if (event->body.type == fURIs.atomBlank)
  249. {
  250. const LV2_Atom_Object* const obj((const LV2_Atom_Object*)&event->body);
  251. if (obj->body.otype != fURIs.timePos)
  252. continue;
  253. LV2_Atom* bar = nullptr;
  254. LV2_Atom* barBeat = nullptr;
  255. LV2_Atom* beatsPerBar = nullptr;
  256. LV2_Atom* bpm = nullptr;
  257. LV2_Atom* beatUnit = nullptr;
  258. LV2_Atom* frame = nullptr;
  259. LV2_Atom* speed = nullptr;
  260. lv2_atom_object_get(obj,
  261. fURIs.timeBar, &bar,
  262. fURIs.timeBarBeat, &barBeat,
  263. fURIs.timeBeatsPerBar, &beatsPerBar,
  264. fURIs.timeBeatsPerMinute, &bpm,
  265. fURIs.timeBeatUnit, &beatUnit,
  266. fURIs.timeFrame, &frame,
  267. fURIs.timeSpeed, &speed,
  268. nullptr);
  269. if (bpm != nullptr && bpm->type == fURIs.atomFloat)
  270. {
  271. fTimeInfo.bbt.beatsPerMinute = ((LV2_Atom_Float*)bpm)->body;
  272. fTimeInfo.bbt.valid = true;
  273. }
  274. if (beatsPerBar != nullptr && beatsPerBar->type == fURIs.atomFloat)
  275. {
  276. float beatsPerBarValue = ((LV2_Atom_Float*)beatsPerBar)->body;
  277. fTimeInfo.bbt.beatsPerBar = beatsPerBarValue;
  278. if (bar != nullptr && bar->type == fURIs.atomLong)
  279. {
  280. //float barValue = ((LV2_Atom_Long*)bar)->body;
  281. //curPosInfo.ppqPositionOfLastBarStart = barValue * beatsPerBarValue;
  282. if (barBeat != nullptr && barBeat->type == fURIs.atomFloat)
  283. {
  284. //float barBeatValue = ((LV2_Atom_Float*)barBeat)->body;
  285. //curPosInfo.ppqPosition = curPosInfo.ppqPositionOfLastBarStart + barBeatValue;
  286. }
  287. }
  288. }
  289. if (beatUnit != nullptr && beatUnit->type == fURIs.atomFloat)
  290. fTimeInfo.bbt.beatType = ((LV2_Atom_Float*)beatUnit)->body;
  291. if (frame != nullptr && frame->type == fURIs.atomLong)
  292. {
  293. const int64_t value(((LV2_Atom_Long*)frame)->body);
  294. CARLA_SAFE_ASSERT_CONTINUE(value >= 0);
  295. fTimeInfo.frame = static_cast<uint64_t>(value);
  296. }
  297. if (speed != nullptr && speed->type == fURIs.atomFloat)
  298. fTimeInfo.playing = carla_compareFloats(((LV2_Atom_Float*)speed)->body, 1.0f);
  299. continue;
  300. }
  301. }
  302. for (uint32_t i=1; i < fDescriptor->midiIns; ++i)
  303. {
  304. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[i], iter)
  305. {
  306. const LV2_Atom_Event* const event((const LV2_Atom_Event*)iter);
  307. if (event == nullptr)
  308. continue;
  309. if (event->body.type != fURIs.midiEvent)
  310. continue;
  311. if (event->body.size > 4)
  312. continue;
  313. if (event->time.frames >= frames)
  314. break;
  315. if (fMidiEventCount >= kMaxMidiEvents*2)
  316. break;
  317. const uint8_t* const data((const uint8_t*)(event + 1));
  318. fMidiEvents[fMidiEventCount].port = (uint8_t)i;
  319. fMidiEvents[fMidiEventCount].size = (uint8_t)event->body.size;
  320. fMidiEvents[fMidiEventCount].time = (uint32_t)event->time.frames;
  321. for (uint32_t j=0; j < event->body.size; ++j)
  322. fMidiEvents[fMidiEventCount].data[j] = data[j];
  323. fMidiEventCount += 1;
  324. }
  325. }
  326. }
  327. fIsProcessing = true;
  328. fDescriptor->process(fHandle, fPorts.audioIns, fPorts.audioOuts, frames, fMidiEvents, fMidiEventCount);
  329. fIsProcessing = false;
  330. // TODO - midi out
  331. updateParameterOutputs();
  332. }
  333. // -------------------------------------------------------------------
  334. uint32_t lv2_get_options(LV2_Options_Option* const /*options*/) const
  335. {
  336. // currently unused
  337. return LV2_OPTIONS_SUCCESS;
  338. }
  339. uint32_t lv2_set_options(const LV2_Options_Option* const options)
  340. {
  341. for (int i=0; options[i].key != 0; ++i)
  342. {
  343. if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  344. {
  345. if (options[i].type == fUridMap->map(fUridMap->handle, LV2_ATOM__Int))
  346. {
  347. const int value(*(const int*)options[i].value);
  348. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  349. fBufferSize = static_cast<uint32_t>(value);
  350. if (fDescriptor->dispatcher != nullptr)
  351. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, value, nullptr, 0.0f);
  352. }
  353. else
  354. carla_stderr("Host changed maxBlockLength but with wrong value type");
  355. }
  356. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_CORE__sampleRate))
  357. {
  358. if (options[i].type == fUridMap->map(fUridMap->handle, LV2_ATOM__Double))
  359. {
  360. const double value(*(const double*)options[i].value);
  361. CARLA_SAFE_ASSERT_CONTINUE(value > 0.0);
  362. fSampleRate = value;
  363. if (fDescriptor->dispatcher != nullptr)
  364. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)fSampleRate);
  365. }
  366. else
  367. carla_stderr("Host changed sampleRate but with wrong value type");
  368. }
  369. }
  370. return LV2_OPTIONS_SUCCESS;
  371. }
  372. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  373. {
  374. if (fDescriptor->category == PLUGIN_CATEGORY_SYNTH)
  375. return nullptr;
  376. if (fDescriptor->get_midi_program_count == nullptr)
  377. return nullptr;
  378. if (fDescriptor->get_midi_program_info == nullptr)
  379. return nullptr;
  380. if (index >= fDescriptor->get_midi_program_count(fHandle))
  381. return nullptr;
  382. const NativeMidiProgram* const midiProg(fDescriptor->get_midi_program_info(fHandle, index));
  383. if (midiProg == nullptr)
  384. return nullptr;
  385. fProgramDesc.bank = midiProg->bank;
  386. fProgramDesc.program = midiProg->program;
  387. fProgramDesc.name = midiProg->name;
  388. return &fProgramDesc;
  389. }
  390. void lv2_select_program(uint32_t bank, uint32_t program)
  391. {
  392. if (fDescriptor->category == PLUGIN_CATEGORY_SYNTH)
  393. return;
  394. if (fDescriptor->set_midi_program == nullptr)
  395. return;
  396. fDescriptor->set_midi_program(fHandle, 0, bank, program);
  397. }
  398. LV2_State_Status lv2_save(const LV2_State_Store_Function store, const LV2_State_Handle handle, const uint32_t /*flags*/, const LV2_Feature* const* const /*features*/) const
  399. {
  400. if ((fDescriptor->hints & PLUGIN_USES_STATE) == 0 || fDescriptor->get_state == nullptr)
  401. return LV2_STATE_ERR_NO_FEATURE;
  402. if (char* const state = fDescriptor->get_state(fHandle))
  403. {
  404. store(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), state, std::strlen(state), fURIs.atomString, LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  405. std::free(state);
  406. return LV2_STATE_SUCCESS;
  407. }
  408. return LV2_STATE_ERR_UNKNOWN;
  409. }
  410. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function retrieve, const LV2_State_Handle handle, uint32_t flags, const LV2_Feature* const* const /*features*/) const
  411. {
  412. if ((fDescriptor->hints & PLUGIN_USES_STATE) == 0 || fDescriptor->set_state == nullptr)
  413. return LV2_STATE_ERR_NO_FEATURE;
  414. size_t size = 0;
  415. uint32_t type = 0;
  416. const void* data = retrieve(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), &size, &type, &flags);
  417. if (size == 0)
  418. return LV2_STATE_ERR_UNKNOWN;
  419. if (type == 0)
  420. return LV2_STATE_ERR_UNKNOWN;
  421. if (data == nullptr)
  422. return LV2_STATE_ERR_UNKNOWN;
  423. if (type != fURIs.atomString)
  424. return LV2_STATE_ERR_BAD_TYPE;
  425. fDescriptor->set_state(fHandle, (const char*)data);
  426. return LV2_STATE_SUCCESS;
  427. }
  428. // -------------------------------------------------------------------
  429. void lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller, LV2UI_Widget* widget, const LV2_Feature* const* features)
  430. {
  431. fUI.writeFunction = writeFunction;
  432. fUI.controller = controller;
  433. // ---------------------------------------------------------------
  434. // see if the host supports external-ui
  435. for (int i=0; features[i] != nullptr; ++i)
  436. {
  437. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  438. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  439. {
  440. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  441. break;
  442. }
  443. }
  444. if (fUI.host != nullptr)
  445. {
  446. fHost.uiName = carla_strdup(fUI.host->plugin_human_id);
  447. *widget = this;
  448. return;
  449. }
  450. // ---------------------------------------------------------------
  451. // no external-ui support, use showInterface
  452. for (int i=0; features[i] != nullptr; ++i)
  453. {
  454. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  455. {
  456. const LV2_Options_Option* const options((const LV2_Options_Option*)features[i]->data);
  457. for (int j=0; options[j].key != 0; ++j)
  458. {
  459. if (options[j].key == fUridMap->map(fUridMap->handle, LV2_UI__windowTitle))
  460. {
  461. fHost.uiName = carla_strdup((const char*)options[j].value);
  462. break;
  463. }
  464. }
  465. break;
  466. }
  467. }
  468. if (fHost.uiName == nullptr)
  469. fHost.uiName = carla_strdup(fDescriptor->name);
  470. *widget = nullptr;
  471. }
  472. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer) const
  473. {
  474. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  475. return;
  476. if (portIndex >= fUI.portOffset || ! fUI.isVisible)
  477. return;
  478. if (fDescriptor->ui_set_parameter_value == nullptr)
  479. return;
  480. const float value(*(const float*)buffer);
  481. fDescriptor->ui_set_parameter_value(fHandle, portIndex-fUI.portOffset, value);
  482. }
  483. void lv2ui_cleanup()
  484. {
  485. fUI.host = nullptr;
  486. fUI.writeFunction = nullptr;
  487. fUI.controller = nullptr;
  488. if (fHost.uiName != nullptr)
  489. {
  490. delete[] fHost.uiName;
  491. fHost.uiName = nullptr;
  492. }
  493. if (! fUI.isVisible)
  494. return;
  495. if (fDescriptor->ui_show != nullptr)
  496. fDescriptor->ui_show(fHandle, false);
  497. fUI.isVisible = false;
  498. }
  499. // -------------------------------------------------------------------
  500. void lv2ui_select_program(uint32_t bank, uint32_t program) const
  501. {
  502. if (fDescriptor->category == PLUGIN_CATEGORY_SYNTH)
  503. return;
  504. if (fDescriptor->ui_set_midi_program == nullptr)
  505. return;
  506. fDescriptor->ui_set_midi_program(fHandle, 0, bank, program);
  507. }
  508. // -------------------------------------------------------------------
  509. int lv2ui_idle() const
  510. {
  511. if (! fUI.isVisible)
  512. return 1;
  513. if (fDescriptor->ui_idle != nullptr)
  514. fDescriptor->ui_idle(fHandle);
  515. return 0;
  516. }
  517. int lv2ui_show()
  518. {
  519. handleUiShow();
  520. return 0;
  521. }
  522. int lv2ui_hide()
  523. {
  524. handleUiHide();
  525. return 0;
  526. }
  527. // -------------------------------------------------------------------
  528. protected:
  529. void handleUiRun() const
  530. {
  531. if (fDescriptor->ui_idle != nullptr)
  532. fDescriptor->ui_idle(fHandle);
  533. }
  534. void handleUiShow()
  535. {
  536. if (fDescriptor->ui_show != nullptr)
  537. fDescriptor->ui_show(fHandle, true);
  538. fUI.isVisible = true;
  539. }
  540. void handleUiHide()
  541. {
  542. if (fDescriptor->ui_show != nullptr)
  543. fDescriptor->ui_show(fHandle, false);
  544. fUI.isVisible = false;
  545. }
  546. // -------------------------------------------------------------------
  547. uint32_t handleGetBufferSize() const
  548. {
  549. return fBufferSize;
  550. }
  551. double handleGetSampleRate() const
  552. {
  553. return fSampleRate;
  554. }
  555. bool handleIsOffline() const
  556. {
  557. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  558. return (fPorts.freewheel != nullptr && *fPorts.freewheel >= 0.5f);
  559. }
  560. const NativeTimeInfo* handleGetTimeInfo() const
  561. {
  562. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  563. return &fTimeInfo;
  564. }
  565. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  566. {
  567. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  568. CARLA_SAFE_ASSERT_RETURN(fDescriptor->midiOuts > 0, false);
  569. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  570. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  571. // reverse-find first free event, and put it there
  572. for (uint32_t i=(kMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  573. {
  574. if (fMidiEvents[i].data[0] == 0)
  575. {
  576. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  577. return true;
  578. }
  579. }
  580. return false;
  581. }
  582. void handleUiParameterChanged(const uint32_t index, const float value) const
  583. {
  584. if (fUI.writeFunction != nullptr && fUI.controller != nullptr)
  585. fUI.writeFunction(fUI.controller, index+fUI.portOffset, sizeof(float), 0, &value);
  586. }
  587. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
  588. {
  589. //storeCustomData(key, value);
  590. }
  591. void handleUiClosed()
  592. {
  593. if (fUI.host != nullptr && fUI.host->ui_closed != nullptr && fUI.controller != nullptr)
  594. fUI.host->ui_closed(fUI.controller);
  595. fUI.host = nullptr;
  596. fUI.writeFunction = nullptr;
  597. fUI.controller = nullptr;
  598. fUI.isVisible = false;
  599. }
  600. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  601. {
  602. // TODO
  603. return nullptr;
  604. }
  605. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  606. {
  607. // TODO
  608. return nullptr;
  609. }
  610. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  611. {
  612. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  613. intptr_t ret = 0;
  614. switch (opcode)
  615. {
  616. case HOST_OPCODE_NULL:
  617. case HOST_OPCODE_UPDATE_PARAMETER:
  618. case HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  619. case HOST_OPCODE_RELOAD_PARAMETERS:
  620. case HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  621. case HOST_OPCODE_RELOAD_ALL:
  622. // nothing
  623. break;
  624. case HOST_OPCODE_UI_UNAVAILABLE:
  625. handleUiClosed();
  626. break;
  627. }
  628. return ret;
  629. // unused for now
  630. (void)index;
  631. (void)value;
  632. (void)ptr;
  633. (void)opt;
  634. }
  635. void updateParameterOutputs()
  636. {
  637. for (uint32_t i=0; i < fPorts.paramCount; ++i)
  638. {
  639. if (fDescriptor->get_parameter_info(fHandle, i)->hints & PARAMETER_IS_OUTPUT)
  640. {
  641. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  642. if (fPorts.paramsPtr[i] != nullptr)
  643. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  644. }
  645. }
  646. }
  647. // -------------------------------------------------------------------
  648. private:
  649. // Native data
  650. NativePluginHandle fHandle;
  651. NativeHostDescriptor fHost;
  652. const NativePluginDescriptor* const fDescriptor;
  653. LV2_Program_Descriptor fProgramDesc;
  654. uint32_t fMidiEventCount;
  655. NativeMidiEvent fMidiEvents[kMaxMidiEvents*2];
  656. NativeTimeInfo fTimeInfo;
  657. bool fIsProcessing;
  658. // Lv2 host data
  659. uint32_t fBufferSize;
  660. double fSampleRate;
  661. const LV2_URID_Map* fUridMap;
  662. struct URIDs {
  663. LV2_URID atomBlank;
  664. LV2_URID atomFloat;
  665. LV2_URID atomLong;
  666. LV2_URID atomSequence;
  667. LV2_URID atomString;
  668. LV2_URID midiEvent;
  669. LV2_URID timePos;
  670. LV2_URID timeBar;
  671. LV2_URID timeBarBeat;
  672. LV2_URID timeBeatsPerBar;
  673. LV2_URID timeBeatsPerMinute;
  674. LV2_URID timeBeatUnit;
  675. LV2_URID timeFrame;
  676. LV2_URID timeSpeed;
  677. URIDs()
  678. : atomBlank(0),
  679. atomFloat(0),
  680. atomLong(0),
  681. atomSequence(0),
  682. atomString(0),
  683. midiEvent(0),
  684. timePos(0),
  685. timeBar(0),
  686. timeBarBeat(0),
  687. timeBeatsPerBar(0),
  688. timeBeatsPerMinute(0),
  689. timeBeatUnit(0),
  690. timeFrame(0),
  691. timeSpeed(0) {}
  692. void map(const LV2_URID_Map* const uridMap)
  693. {
  694. atomBlank = uridMap->map(uridMap->handle, LV2_ATOM__Blank);
  695. atomFloat = uridMap->map(uridMap->handle, LV2_ATOM__Float);
  696. atomLong = uridMap->map(uridMap->handle, LV2_ATOM__Long);
  697. atomSequence = uridMap->map(uridMap->handle, LV2_ATOM__Sequence);
  698. atomString = uridMap->map(uridMap->handle, LV2_ATOM__String);
  699. midiEvent = uridMap->map(uridMap->handle, LV2_MIDI__MidiEvent);
  700. timePos = uridMap->map(uridMap->handle, LV2_TIME__Position);
  701. timeBar = uridMap->map(uridMap->handle, LV2_TIME__bar);
  702. timeBarBeat = uridMap->map(uridMap->handle, LV2_TIME__barBeat);
  703. timeBeatUnit = uridMap->map(uridMap->handle, LV2_TIME__beatUnit);
  704. timeFrame = uridMap->map(uridMap->handle, LV2_TIME__frame);
  705. timeSpeed = uridMap->map(uridMap->handle, LV2_TIME__speed);
  706. timeBeatsPerBar = uridMap->map(uridMap->handle, LV2_TIME__beatsPerBar);
  707. timeBeatsPerMinute = uridMap->map(uridMap->handle, LV2_TIME__beatsPerMinute);
  708. }
  709. } fURIs;
  710. struct UI {
  711. const LV2_External_UI_Host* host;
  712. LV2UI_Write_Function writeFunction;
  713. LV2UI_Controller controller;
  714. uint32_t portOffset;
  715. bool isVisible;
  716. UI()
  717. : host(nullptr),
  718. writeFunction(nullptr),
  719. controller(nullptr),
  720. portOffset(0),
  721. isVisible(false) {}
  722. } fUI;
  723. struct Ports {
  724. LV2_Atom_Sequence** eventsIn;
  725. LV2_Atom_Sequence** midiOuts;
  726. float** audioIns;
  727. float** audioOuts;
  728. float* freewheel;
  729. uint32_t paramCount;
  730. float* paramsLast;
  731. float** paramsPtr;
  732. Ports()
  733. : eventsIn(nullptr),
  734. midiOuts(nullptr),
  735. audioIns(nullptr),
  736. audioOuts(nullptr),
  737. freewheel(nullptr),
  738. paramCount(0),
  739. paramsLast(nullptr),
  740. paramsPtr(nullptr) {}
  741. ~Ports()
  742. {
  743. if (eventsIn != nullptr)
  744. {
  745. delete[] eventsIn;
  746. eventsIn = nullptr;
  747. }
  748. if (midiOuts != nullptr)
  749. {
  750. delete[] midiOuts;
  751. midiOuts = nullptr;
  752. }
  753. if (audioIns != nullptr)
  754. {
  755. delete[] audioIns;
  756. audioIns = nullptr;
  757. }
  758. if (audioOuts != nullptr)
  759. {
  760. delete[] audioOuts;
  761. audioOuts = nullptr;
  762. }
  763. if (paramsLast != nullptr)
  764. {
  765. delete[] paramsLast;
  766. paramsLast = nullptr;
  767. }
  768. if (paramsPtr != nullptr)
  769. {
  770. delete[] paramsPtr;
  771. paramsPtr = nullptr;
  772. }
  773. }
  774. void init(const NativePluginDescriptor* const desc, NativePluginHandle handle)
  775. {
  776. CARLA_SAFE_ASSERT_RETURN(desc != nullptr && handle != nullptr,)
  777. if (desc->midiIns > 0)
  778. {
  779. eventsIn = new LV2_Atom_Sequence*[desc->midiIns];
  780. for (uint32_t i=0; i < desc->midiIns; ++i)
  781. eventsIn[i] = nullptr;
  782. }
  783. else if (desc->hints & PLUGIN_USES_TIME)
  784. {
  785. eventsIn = new LV2_Atom_Sequence*[1];
  786. eventsIn[0] = nullptr;
  787. }
  788. if (desc->midiOuts > 0)
  789. {
  790. midiOuts = new LV2_Atom_Sequence*[desc->midiOuts];
  791. for (uint32_t i=0; i < desc->midiOuts; ++i)
  792. midiOuts[i] = nullptr;
  793. }
  794. if (desc->audioIns > 0)
  795. {
  796. audioIns = new float*[desc->audioIns];
  797. for (uint32_t i=0; i < desc->audioIns; ++i)
  798. audioIns[i] = nullptr;
  799. }
  800. if (desc->audioOuts > 0)
  801. {
  802. audioOuts = new float*[desc->audioOuts];
  803. for (uint32_t i=0; i < desc->audioOuts; ++i)
  804. audioOuts[i] = nullptr;
  805. }
  806. if (desc->get_parameter_count != nullptr && desc->get_parameter_info != nullptr && desc->get_parameter_value != nullptr && desc->set_parameter_value != nullptr)
  807. {
  808. paramCount = desc->get_parameter_count(handle);
  809. if (paramCount > 0)
  810. {
  811. paramsLast = new float[paramCount];
  812. paramsPtr = new float*[paramCount];
  813. for (uint32_t i=0; i < paramCount; ++i)
  814. {
  815. paramsLast[i] = desc->get_parameter_value(handle, i);
  816. paramsPtr[i] = nullptr;
  817. }
  818. }
  819. }
  820. }
  821. void connectPort(const NativePluginDescriptor* const desc, const uint32_t port, void* const dataLocation)
  822. {
  823. uint32_t index = 0;
  824. if (desc->midiIns > 0 || (desc->hints & PLUGIN_USES_TIME) != 0)
  825. {
  826. if (port == index++)
  827. {
  828. eventsIn[0] = (LV2_Atom_Sequence*)dataLocation;
  829. return;
  830. }
  831. }
  832. for (uint32_t i=1; i < desc->midiIns; ++i)
  833. {
  834. if (port == index++)
  835. {
  836. eventsIn[i] = (LV2_Atom_Sequence*)dataLocation;
  837. return;
  838. }
  839. }
  840. for (uint32_t i=0; i < desc->midiOuts; ++i)
  841. {
  842. if (port == index++)
  843. {
  844. midiOuts[i] = (LV2_Atom_Sequence*)dataLocation;
  845. return;
  846. }
  847. }
  848. if (port == index++)
  849. {
  850. freewheel = (float*)dataLocation;
  851. return;
  852. }
  853. for (uint32_t i=0; i < desc->audioIns; ++i)
  854. {
  855. if (port == index++)
  856. {
  857. audioIns[i] = (float*)dataLocation;
  858. return;
  859. }
  860. }
  861. for (uint32_t i=0; i < desc->audioOuts; ++i)
  862. {
  863. if (port == index++)
  864. {
  865. audioOuts[i] = (float*)dataLocation;
  866. return;
  867. }
  868. }
  869. for (uint32_t i=0; i < paramCount; ++i)
  870. {
  871. if (port == index++)
  872. {
  873. paramsPtr[i] = (float*)dataLocation;
  874. return;
  875. }
  876. }
  877. }
  878. CARLA_DECLARE_NON_COPY_STRUCT(Ports);
  879. } fPorts;
  880. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  881. SharedResourcePointer<ScopedJuceInitialiser_GUI> sJuceGUI;
  882. #endif
  883. // -------------------------------------------------------------------
  884. #define handlePtr ((NativePlugin*)self)
  885. static void extui_run(LV2_External_UI_Widget_Compat* self)
  886. {
  887. handlePtr->handleUiRun();
  888. }
  889. static void extui_show(LV2_External_UI_Widget_Compat* self)
  890. {
  891. handlePtr->handleUiShow();
  892. }
  893. static void extui_hide(LV2_External_UI_Widget_Compat* self)
  894. {
  895. handlePtr->handleUiHide();
  896. }
  897. #undef handlePtr
  898. // -------------------------------------------------------------------
  899. #define handlePtr ((NativePlugin*)handle)
  900. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  901. {
  902. return handlePtr->handleGetBufferSize();
  903. }
  904. static double host_get_sample_rate(NativeHostHandle handle)
  905. {
  906. return handlePtr->handleGetSampleRate();
  907. }
  908. static bool host_is_offline(NativeHostHandle handle)
  909. {
  910. return handlePtr->handleIsOffline();
  911. }
  912. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  913. {
  914. return handlePtr->handleGetTimeInfo();
  915. }
  916. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  917. {
  918. return handlePtr->handleWriteMidiEvent(event);
  919. }
  920. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  921. {
  922. handlePtr->handleUiParameterChanged(index, value);
  923. }
  924. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  925. {
  926. handlePtr->handleUiCustomDataChanged(key, value);
  927. }
  928. static void host_ui_closed(NativeHostHandle handle)
  929. {
  930. handlePtr->handleUiClosed();
  931. }
  932. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  933. {
  934. return handlePtr->handleUiOpenFile(isDir, title, filter);
  935. }
  936. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  937. {
  938. return handlePtr->handleUiSaveFile(isDir, title, filter);
  939. }
  940. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  941. {
  942. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  943. }
  944. #undef handlePtr
  945. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  946. };
  947. // -----------------------------------------------------------------------
  948. // LV2 plugin descriptor functions
  949. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  950. {
  951. carla_debug("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  952. const NativePluginDescriptor* pluginDesc = nullptr;
  953. const char* pluginLabel = nullptr;
  954. if (std::strncmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla/plugins/", 37) == 0)
  955. pluginLabel = lv2Descriptor->URI+37;
  956. if (pluginLabel == nullptr)
  957. {
  958. carla_stderr("Failed to find carla native plugin with URI \"%s\"", lv2Descriptor->URI);
  959. return nullptr;
  960. }
  961. carla_debug("lv2_instantiate() - looking up label \"%s\"", pluginLabel);
  962. PluginListManager& plm(PluginListManager::getInstance());
  963. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin(); it.valid(); it.next())
  964. {
  965. const NativePluginDescriptor* const& tmpDesc(it.getValue());
  966. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  967. {
  968. pluginDesc = tmpDesc;
  969. break;
  970. }
  971. }
  972. if (pluginDesc == nullptr)
  973. {
  974. carla_stderr("Failed to find carla native plugin with label \"%s\"", pluginLabel);
  975. return nullptr;
  976. }
  977. NativePlugin* const plugin(new NativePlugin(pluginDesc, sampleRate, bundlePath, features));
  978. if (! plugin->init())
  979. {
  980. carla_stderr("Failed to init plugin");
  981. delete plugin;
  982. return nullptr;
  983. }
  984. return (LV2_Handle)plugin;
  985. }
  986. #define instancePtr ((NativePlugin*)instance)
  987. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  988. {
  989. instancePtr->lv2_connect_port(port, dataLocation);
  990. }
  991. static void lv2_activate(LV2_Handle instance)
  992. {
  993. carla_debug("lv2_activate(%p)", instance);
  994. instancePtr->lv2_activate();
  995. }
  996. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  997. {
  998. instancePtr->lv2_run(sampleCount);
  999. }
  1000. static void lv2_deactivate(LV2_Handle instance)
  1001. {
  1002. carla_debug("lv2_deactivate(%p)", instance);
  1003. instancePtr->lv2_deactivate();
  1004. }
  1005. static void lv2_cleanup(LV2_Handle instance)
  1006. {
  1007. carla_debug("lv2_cleanup(%p)", instance);
  1008. instancePtr->lv2_cleanup();
  1009. delete instancePtr;
  1010. }
  1011. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  1012. {
  1013. carla_debug("lv2_get_options(%p, %p)", instance, options);
  1014. return instancePtr->lv2_get_options(options);
  1015. }
  1016. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  1017. {
  1018. carla_debug("lv2_set_options(%p, %p)", instance, options);
  1019. return instancePtr->lv2_set_options(options);
  1020. }
  1021. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  1022. {
  1023. carla_debug("lv2_get_program(%p, %i)", instance, index);
  1024. return instancePtr->lv2_get_program(index);
  1025. }
  1026. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  1027. {
  1028. carla_debug("lv2_select_program(%p, %i, %i)", instance, bank, program);
  1029. return instancePtr->lv2_select_program(bank, program);
  1030. }
  1031. static LV2_State_Status lv2_save(LV2_Handle instance, LV2_State_Store_Function store, LV2_State_Handle handle, uint32_t flags, const LV2_Feature* const* features)
  1032. {
  1033. carla_debug("lv2_save(%p, %p, %p, %i, %p)", instance, store, handle, flags, features);
  1034. return instancePtr->lv2_save(store, handle, flags, features);
  1035. }
  1036. static LV2_State_Status lv2_restore(LV2_Handle instance, LV2_State_Retrieve_Function retrieve, LV2_State_Handle handle, uint32_t flags, const LV2_Feature* const* features)
  1037. {
  1038. carla_debug("lv2_restore(%p, %p, %p, %i, %p)", instance, retrieve, handle, flags, features);
  1039. return instancePtr->lv2_restore(retrieve, handle, flags, features);
  1040. }
  1041. static const void* lv2_extension_data(const char* uri)
  1042. {
  1043. carla_debug("lv2_extension_data(\"%s\")", uri);
  1044. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  1045. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  1046. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  1047. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  1048. return &options;
  1049. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  1050. return &programs;
  1051. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  1052. return &state;
  1053. return nullptr;
  1054. }
  1055. #undef instancePtr
  1056. // -----------------------------------------------------------------------
  1057. // LV2 UI descriptor functions
  1058. static LV2UI_Handle lv2ui_instantiate(const LV2UI_Descriptor*, const char*, const char*, LV2UI_Write_Function writeFunction,
  1059. LV2UI_Controller controller, LV2UI_Widget* widget, const LV2_Feature* const* features)
  1060. {
  1061. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  1062. NativePlugin* plugin = nullptr;
  1063. for (int i=0; features[i] != nullptr; ++i)
  1064. {
  1065. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  1066. {
  1067. plugin = (NativePlugin*)features[i]->data;
  1068. break;
  1069. }
  1070. }
  1071. if (plugin == nullptr)
  1072. {
  1073. carla_stderr("Host doesn't support instance-access, cannot show UI");
  1074. return nullptr;
  1075. }
  1076. plugin->lv2ui_instantiate(writeFunction, controller, widget, features);
  1077. return (LV2UI_Handle)plugin;
  1078. }
  1079. #define uiPtr ((NativePlugin*)ui)
  1080. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  1081. {
  1082. carla_debug("lv2ui_port_event(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  1083. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  1084. }
  1085. static void lv2ui_cleanup(LV2UI_Handle ui)
  1086. {
  1087. carla_debug("lv2ui_cleanup(%p)", ui);
  1088. uiPtr->lv2ui_cleanup();
  1089. }
  1090. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  1091. {
  1092. carla_debug("lv2ui_select_program(%p, %i, %i)", ui, bank, program);
  1093. uiPtr->lv2ui_select_program(bank, program);
  1094. }
  1095. static int lv2ui_idle(LV2UI_Handle ui)
  1096. {
  1097. return uiPtr->lv2ui_idle();
  1098. }
  1099. static int lv2ui_show(LV2UI_Handle ui)
  1100. {
  1101. carla_debug("lv2ui_show(%p)", ui);
  1102. return uiPtr->lv2ui_show();
  1103. }
  1104. static int lv2ui_hide(LV2UI_Handle ui)
  1105. {
  1106. carla_debug("lv2ui_hide(%p)", ui);
  1107. return uiPtr->lv2ui_hide();
  1108. }
  1109. static const void* lv2ui_extension_data(const char* uri)
  1110. {
  1111. carla_stdout("lv2ui_extension_data(\"%s\")", uri);
  1112. static const LV2UI_Idle_Interface uiidle = { lv2ui_idle };
  1113. static const LV2UI_Show_Interface uishow = { lv2ui_show, lv2ui_hide };
  1114. static const LV2_Programs_UI_Interface uiprograms = { lv2ui_select_program };
  1115. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  1116. return &uiidle;
  1117. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  1118. return &uishow;
  1119. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  1120. return &uiprograms;
  1121. return nullptr;
  1122. }
  1123. #undef uiPtr
  1124. // -----------------------------------------------------------------------
  1125. // Startup code
  1126. CARLA_EXPORT
  1127. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  1128. {
  1129. carla_debug("lv2_descriptor(%i)", index);
  1130. PluginListManager& plm(PluginListManager::getInstance());
  1131. if (index >= plm.descs.count())
  1132. {
  1133. carla_debug("lv2_descriptor(%i) - out of bounds", index);
  1134. return nullptr;
  1135. }
  1136. if (index < plm.lv2Descs.count())
  1137. {
  1138. carla_debug("lv2_descriptor(%i) - found previously allocated", index);
  1139. return plm.lv2Descs.getAt(index, nullptr);
  1140. }
  1141. const NativePluginDescriptor* const pluginDesc(plm.descs.getAt(index, nullptr));
  1142. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, nullptr);
  1143. CarlaString tmpURI;
  1144. tmpURI = "http://kxstudio.sf.net/carla/plugins/";
  1145. tmpURI += pluginDesc->label;
  1146. carla_debug("lv2_descriptor(%i) - not found, allocating new with uri \"%s\"", index, (const char*)tmpURI);
  1147. const LV2_Descriptor lv2DescTmp = {
  1148. /* URI */ carla_strdup(tmpURI),
  1149. /* instantiate */ lv2_instantiate,
  1150. /* connect_port */ lv2_connect_port,
  1151. /* activate */ lv2_activate,
  1152. /* run */ lv2_run,
  1153. /* deactivate */ lv2_deactivate,
  1154. /* cleanup */ lv2_cleanup,
  1155. /* extension_data */ lv2_extension_data
  1156. };
  1157. LV2_Descriptor* const lv2Desc(new LV2_Descriptor);
  1158. std::memcpy(lv2Desc, &lv2DescTmp, sizeof(LV2_Descriptor));
  1159. plm.lv2Descs.append(lv2Desc);
  1160. return lv2Desc;
  1161. }
  1162. CARLA_EXPORT
  1163. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  1164. {
  1165. carla_debug("lv2ui_descriptor(%i)", index);
  1166. static const LV2UI_Descriptor lv2UiDesc = {
  1167. /* URI */ "http://kxstudio.sf.net/carla/ui",
  1168. /* instantiate */ lv2ui_instantiate,
  1169. /* cleanup */ lv2ui_cleanup,
  1170. /* port_event */ lv2ui_port_event,
  1171. /* extension_data */ lv2ui_extension_data
  1172. };
  1173. return (index == 0) ? &lv2UiDesc : nullptr;
  1174. }
  1175. // -----------------------------------------------------------------------