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.

1030 lines
33KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2017 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #define CARLA_NATIVE_PLUGIN_LV2
  18. #include "carla-base.cpp"
  19. #include "CarlaLv2Utils.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #include "CarlaString.hpp"
  22. // --------------------------------------------------------------------------------------------------------------------
  23. // Carla Internal Plugin API exposed as LV2 plugin
  24. class NativePlugin : public Lv2PluginBaseClass<NativeTimeInfo>
  25. {
  26. public:
  27. static const uint32_t kMaxMidiEvents = 512;
  28. NativePlugin(const NativePluginDescriptor* const desc,
  29. const double sampleRate,
  30. const char* const bundlePath,
  31. const LV2_Feature* const* const features)
  32. : Lv2PluginBaseClass(sampleRate, features),
  33. fHandle(nullptr),
  34. fHost(),
  35. fDescriptor(desc),
  36. #ifdef CARLA_PROPER_CPP11_SUPPORT
  37. fProgramDesc({0, 0, nullptr}),
  38. #endif
  39. fMidiEventCount(0)
  40. {
  41. carla_zeroStruct(fHost);
  42. if (! loadedInProperHost())
  43. return;
  44. CarlaString resourceDir(bundlePath);
  45. resourceDir += CARLA_OS_SEP_STR "resources" CARLA_OS_SEP_STR;
  46. fHost.handle = this;
  47. fHost.resourceDir = resourceDir.dupSafe();
  48. fHost.uiName = nullptr;
  49. fHost.uiParentId = 0;
  50. fHost.get_buffer_size = host_get_buffer_size;
  51. fHost.get_sample_rate = host_get_sample_rate;
  52. fHost.is_offline = host_is_offline;
  53. fHost.get_time_info = host_get_time_info;
  54. fHost.write_midi_event = host_write_midi_event;
  55. fHost.ui_parameter_changed = host_ui_parameter_changed;
  56. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  57. fHost.ui_closed = host_ui_closed;
  58. fHost.ui_open_file = host_ui_open_file;
  59. fHost.ui_save_file = host_ui_save_file;
  60. fHost.dispatcher = host_dispatcher;
  61. }
  62. ~NativePlugin()
  63. {
  64. CARLA_SAFE_ASSERT(fHandle == nullptr);
  65. if (fHost.resourceDir != nullptr)
  66. {
  67. delete[] fHost.resourceDir;
  68. fHost.resourceDir = nullptr;
  69. }
  70. if (fHost.uiName != nullptr)
  71. {
  72. delete[] fHost.uiName;
  73. fHost.uiName = nullptr;
  74. }
  75. }
  76. // ----------------------------------------------------------------------------------------------------------------
  77. bool init()
  78. {
  79. if (fHost.resourceDir == nullptr)
  80. return false;
  81. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  82. {
  83. carla_stderr("Plugin is missing something...");
  84. return false;
  85. }
  86. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  87. fHandle = fDescriptor->instantiate(&fHost);
  88. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  89. fPorts.usesTime = fDescriptor->hints & NATIVE_PLUGIN_USES_TIME;
  90. fPorts.numAudioIns = fDescriptor->audioIns;
  91. fPorts.numAudioOuts = fDescriptor->audioOuts;
  92. fPorts.numMidiIns = fDescriptor->midiIns;
  93. fPorts.numMidiOuts = fDescriptor->midiOuts;
  94. if (fDescriptor->get_parameter_count != nullptr &&
  95. fDescriptor->get_parameter_info != nullptr &&
  96. fDescriptor->get_parameter_value != nullptr &&
  97. fDescriptor->set_parameter_value != nullptr)
  98. {
  99. fPorts.numParams = fDescriptor->get_parameter_count(fHandle);
  100. }
  101. fPorts.init();
  102. if (fPorts.numParams > 0)
  103. {
  104. for (uint32_t i=0; i < fPorts.numParams; ++i)
  105. {
  106. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  107. fPorts.paramsOut [i] = fDescriptor->get_parameter_info(fHandle, i)->hints & NATIVE_PARAMETER_IS_OUTPUT;
  108. }
  109. }
  110. return true;
  111. }
  112. // ----------------------------------------------------------------------------------------------------------------
  113. // LV2 functions
  114. void lv2_activate()
  115. {
  116. CARLA_SAFE_ASSERT_RETURN(! fIsActive,);
  117. resetTimeInfo();
  118. if (fDescriptor->activate != nullptr)
  119. fDescriptor->activate(fHandle);
  120. fIsActive = true;
  121. }
  122. void lv2_deactivate()
  123. {
  124. CARLA_SAFE_ASSERT_RETURN(fIsActive,);
  125. fIsActive = false;
  126. if (fDescriptor->deactivate != nullptr)
  127. fDescriptor->deactivate(fHandle);
  128. }
  129. void lv2_cleanup()
  130. {
  131. if (fIsActive)
  132. {
  133. carla_stderr("Warning: Host forgot to call deactivate!");
  134. fIsActive = false;
  135. if (fDescriptor->deactivate != nullptr)
  136. fDescriptor->deactivate(fHandle);
  137. }
  138. if (fDescriptor->cleanup != nullptr)
  139. fDescriptor->cleanup(fHandle);
  140. fHandle = nullptr;
  141. }
  142. // ----------------------------------------------------------------------------------------------------------------
  143. void lv2_run(const uint32_t frames)
  144. {
  145. if (! lv2_pre_run(frames))
  146. {
  147. updateParameterOutputs();
  148. return;
  149. }
  150. if (fPorts.numMidiIns > 0)
  151. {
  152. fMidiEventCount = 0;
  153. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  154. for (uint32_t i=0; i < fPorts.numMidiIns; ++i)
  155. {
  156. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[i], event)
  157. {
  158. if (event == nullptr)
  159. continue;
  160. if (event->body.type != fURIs.midiEvent)
  161. continue;
  162. if (event->body.size > 4)
  163. continue;
  164. if (event->time.frames >= frames)
  165. break;
  166. const uint8_t* const data((const uint8_t*)(event + 1));
  167. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  168. nativeEvent.port = (uint8_t)i;
  169. nativeEvent.size = (uint8_t)event->body.size;
  170. nativeEvent.time = (uint32_t)event->time.frames;
  171. uint32_t j=0;
  172. for (uint32_t size=event->body.size; j<size; ++j)
  173. nativeEvent.data[j] = data[j];
  174. for (; j<4; ++j)
  175. nativeEvent.data[j] = 0;
  176. if (fMidiEventCount >= kMaxMidiEvents)
  177. break;
  178. }
  179. }
  180. }
  181. // FIXME
  182. fDescriptor->process(fHandle,
  183. const_cast<float**>(fPorts.audioIns), fPorts.audioOuts, frames,
  184. fMidiEvents, fMidiEventCount);
  185. lv2_post_run(frames);
  186. updateParameterOutputs();
  187. }
  188. // ----------------------------------------------------------------------------------------------------------------
  189. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  190. {
  191. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  192. return nullptr;
  193. if (fDescriptor->get_midi_program_count == nullptr)
  194. return nullptr;
  195. if (fDescriptor->get_midi_program_info == nullptr)
  196. return nullptr;
  197. if (index >= fDescriptor->get_midi_program_count(fHandle))
  198. return nullptr;
  199. const NativeMidiProgram* const midiProg(fDescriptor->get_midi_program_info(fHandle, index));
  200. if (midiProg == nullptr)
  201. return nullptr;
  202. fProgramDesc.bank = midiProg->bank;
  203. fProgramDesc.program = midiProg->program;
  204. fProgramDesc.name = midiProg->name;
  205. return &fProgramDesc;
  206. }
  207. void lv2_select_program(uint32_t bank, uint32_t program)
  208. {
  209. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  210. return;
  211. if (fDescriptor->set_midi_program == nullptr)
  212. return;
  213. fDescriptor->set_midi_program(fHandle, 0, bank, program);
  214. for (uint32_t i=0; i < fPorts.numParams; ++i)
  215. {
  216. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  217. if (fPorts.paramsPtr[i] != nullptr)
  218. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  219. }
  220. }
  221. // ----------------------------------------------------------------------------------------------------------------
  222. LV2_State_Status lv2_save(const LV2_State_Store_Function store, const LV2_State_Handle handle,
  223. const uint32_t /*flags*/, const LV2_Feature* const* const /*features*/) const
  224. {
  225. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->get_state == nullptr)
  226. return LV2_STATE_ERR_NO_FEATURE;
  227. if (char* const state = fDescriptor->get_state(fHandle))
  228. {
  229. store(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), state, std::strlen(state)+1, fURIs.atomString, LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  230. std::free(state);
  231. return LV2_STATE_SUCCESS;
  232. }
  233. return LV2_STATE_ERR_UNKNOWN;
  234. }
  235. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function retrieve, const LV2_State_Handle handle,
  236. uint32_t flags, const LV2_Feature* const* const /*features*/) const
  237. {
  238. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->set_state == nullptr)
  239. return LV2_STATE_ERR_NO_FEATURE;
  240. size_t size = 0;
  241. uint32_t type = 0;
  242. const void* const data = retrieve(handle, fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"), &size, &type, &flags);
  243. if (size == 0)
  244. return LV2_STATE_ERR_UNKNOWN;
  245. if (type == 0)
  246. return LV2_STATE_ERR_UNKNOWN;
  247. if (data == nullptr)
  248. return LV2_STATE_ERR_UNKNOWN;
  249. if (type != fURIs.atomString)
  250. return LV2_STATE_ERR_BAD_TYPE;
  251. fDescriptor->set_state(fHandle, (const char*)data);
  252. return LV2_STATE_SUCCESS;
  253. }
  254. // ----------------------------------------------------------------------------------------------------------------
  255. void lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  256. LV2UI_Widget* widget, const LV2_Feature* const* features, const bool isEmbed)
  257. {
  258. fUI.writeFunction = writeFunction;
  259. fUI.controller = controller;
  260. fUI.isEmbed = isEmbed;
  261. if (fHost.uiName != nullptr)
  262. {
  263. delete[] fHost.uiName;
  264. fHost.uiName = nullptr;
  265. }
  266. #ifdef CARLA_OS_LINUX
  267. // ---------------------------------------------------------------
  268. // show embed UI if needed
  269. if (isEmbed)
  270. {
  271. intptr_t parentId = 0;
  272. const LV2UI_Resize* uiResize = nullptr;
  273. for (int i=0; features[i] != nullptr; ++i)
  274. {
  275. if (std::strcmp(features[i]->URI, LV2_UI__parent) == 0)
  276. {
  277. parentId = (intptr_t)features[i]->data;
  278. }
  279. else if (std::strcmp(features[i]->URI, LV2_UI__resize) == 0)
  280. {
  281. uiResize = (const LV2UI_Resize*)features[i]->data;
  282. }
  283. }
  284. // -----------------------------------------------------------
  285. // see if the host can really embed the UI
  286. if (parentId != 0)
  287. {
  288. // wait for remote side to be ready
  289. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_NULL, (int32_t)0xDEADF00D, 0xC0C0B00B, nullptr, 0.0f);
  290. if (uiResize && uiResize->ui_resize != nullptr)
  291. uiResize->ui_resize(uiResize->handle, 740, 512);
  292. fHost.uiName = carla_strdup(fDescriptor->name);
  293. fUI.isVisible = true;
  294. char strBuf[0xff+1];
  295. strBuf[0xff] = '\0';
  296. std::snprintf(strBuf, 0xff, P_INTPTR, parentId);
  297. carla_setenv("CARLA_PLUGIN_EMBED_WINID", strBuf);
  298. fDescriptor->ui_show(fHandle, true);
  299. carla_setenv("CARLA_PLUGIN_EMBED_WINID", "0");
  300. const intptr_t winId(fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_NULL, (int32_t)0xDEADF00D, 0xC0C0B00B, nullptr, 0.0f));
  301. CARLA_SAFE_ASSERT_RETURN(winId != 0,);
  302. *widget = (LV2UI_Widget)winId;
  303. return;
  304. }
  305. }
  306. #endif
  307. // ---------------------------------------------------------------
  308. // see if the host supports external-ui
  309. for (int i=0; features[i] != nullptr; ++i)
  310. {
  311. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  312. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  313. {
  314. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  315. break;
  316. }
  317. }
  318. if (fUI.host != nullptr)
  319. {
  320. fHost.uiName = carla_strdup(fUI.host->plugin_human_id);
  321. *widget = (LV2_External_UI_Widget_Compat*)this;
  322. return;
  323. }
  324. // ---------------------------------------------------------------
  325. // no external-ui support, use showInterface
  326. for (int i=0; features[i] != nullptr; ++i)
  327. {
  328. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  329. {
  330. const LV2_Options_Option* const options((const LV2_Options_Option*)features[i]->data);
  331. for (int j=0; options[j].key != 0; ++j)
  332. {
  333. if (options[j].key == fUridMap->map(fUridMap->handle, LV2_UI__windowTitle))
  334. {
  335. fHost.uiName = carla_strdup((const char*)options[j].value);
  336. break;
  337. }
  338. }
  339. break;
  340. }
  341. }
  342. if (fHost.uiName == nullptr)
  343. fHost.uiName = carla_strdup(fDescriptor->name);
  344. *widget = nullptr;
  345. }
  346. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer) const
  347. {
  348. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  349. return;
  350. if (portIndex >= fPorts.indexOffset || ! fUI.isVisible)
  351. return;
  352. if (fDescriptor->ui_set_parameter_value == nullptr)
  353. return;
  354. const float value(*(const float*)buffer);
  355. fDescriptor->ui_set_parameter_value(fHandle, portIndex-fPorts.indexOffset, value);
  356. }
  357. // ----------------------------------------------------------------------------------------------------------------
  358. void lv2ui_select_program(uint32_t bank, uint32_t program) const
  359. {
  360. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  361. return;
  362. if (fDescriptor->ui_set_midi_program == nullptr)
  363. return;
  364. fDescriptor->ui_set_midi_program(fHandle, 0, bank, program);
  365. }
  366. // ----------------------------------------------------------------------------------------------------------------
  367. protected:
  368. void handleUiRun() const override
  369. {
  370. if (fDescriptor->ui_idle != nullptr)
  371. fDescriptor->ui_idle(fHandle);
  372. }
  373. void handleUiShow() override
  374. {
  375. if (fDescriptor->ui_show != nullptr)
  376. fDescriptor->ui_show(fHandle, true);
  377. fUI.isVisible = true;
  378. }
  379. void handleUiHide() override
  380. {
  381. if (fDescriptor->ui_show != nullptr)
  382. fDescriptor->ui_show(fHandle, false);
  383. fUI.isVisible = false;
  384. }
  385. // ----------------------------------------------------------------------------------------------------------------
  386. void handleParameterValueChanged(const uint32_t index, const float value) override
  387. {
  388. fDescriptor->set_parameter_value(fHandle, index, value);
  389. }
  390. void handleBufferSizeChanged(const uint32_t bufferSize) override
  391. {
  392. if (fDescriptor->dispatcher == nullptr)
  393. return;
  394. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, bufferSize, nullptr, 0.0f);
  395. }
  396. void handleSampleRateChanged(const double sampleRate) override
  397. {
  398. if (fDescriptor->dispatcher == nullptr)
  399. return;
  400. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)sampleRate);
  401. }
  402. // ----------------------------------------------------------------------------------------------------------------
  403. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  404. {
  405. CARLA_SAFE_ASSERT_RETURN(fPorts.numMidiOuts > 0, false);
  406. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  407. CARLA_SAFE_ASSERT_RETURN(event->size > 0, false);
  408. const uint8_t port(event->port);
  409. CARLA_SAFE_ASSERT_RETURN(port < fPorts.numMidiOuts, false);
  410. LV2_Atom_Sequence* const seq(fPorts.midiOuts[port]);
  411. CARLA_SAFE_ASSERT_RETURN(seq != nullptr, false);
  412. Ports::MidiOutData& mData(fPorts.midiOutData[port]);
  413. if (sizeof(LV2_Atom_Event) + event->size > mData.capacity - mData.offset)
  414. return false;
  415. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, seq) + mData.offset);
  416. aev->time.frames = event->time;
  417. aev->body.size = event->size;
  418. aev->body.type = fURIs.midiEvent;
  419. std::memcpy(LV2_ATOM_BODY(&aev->body), event->data, event->size);
  420. const uint32_t size = lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom_Event) + event->size));
  421. mData.offset += size;
  422. seq->atom.size += size;
  423. return true;
  424. }
  425. void handleUiParameterChanged(const uint32_t index, const float value) const
  426. {
  427. if (fUI.writeFunction != nullptr && fUI.controller != nullptr)
  428. fUI.writeFunction(fUI.controller, index+fPorts.indexOffset, sizeof(float), 0, &value);
  429. }
  430. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
  431. {
  432. //storeCustomData(key, value);
  433. }
  434. void handleUiClosed()
  435. {
  436. if (fUI.host != nullptr && fUI.host->ui_closed != nullptr && fUI.controller != nullptr)
  437. fUI.host->ui_closed(fUI.controller);
  438. fUI.host = nullptr;
  439. fUI.writeFunction = nullptr;
  440. fUI.controller = nullptr;
  441. fUI.isVisible = false;
  442. }
  443. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  444. {
  445. // TODO
  446. return nullptr;
  447. }
  448. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  449. {
  450. // TODO
  451. return nullptr;
  452. }
  453. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  454. {
  455. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  456. intptr_t ret = 0;
  457. switch (opcode)
  458. {
  459. case NATIVE_HOST_OPCODE_NULL:
  460. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  461. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  462. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  463. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  464. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  465. case NATIVE_HOST_OPCODE_HOST_IDLE:
  466. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  467. // nothing
  468. break;
  469. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  470. handleUiClosed();
  471. break;
  472. }
  473. return ret;
  474. // unused for now
  475. (void)index;
  476. (void)value;
  477. (void)ptr;
  478. (void)opt;
  479. }
  480. void updateParameterOutputs()
  481. {
  482. float value;
  483. for (uint32_t i=0; i < fPorts.numParams; ++i)
  484. {
  485. if (! fPorts.paramsOut[i])
  486. continue;
  487. fPorts.paramsLast[i] = value = fDescriptor->get_parameter_value(fHandle, i);
  488. if (fPorts.paramsPtr[i] != nullptr)
  489. *fPorts.paramsPtr[i] = value;
  490. }
  491. }
  492. // -------------------------------------------------------------------
  493. private:
  494. // Native data
  495. NativePluginHandle fHandle;
  496. NativeHostDescriptor fHost;
  497. const NativePluginDescriptor* const fDescriptor;
  498. LV2_Program_Descriptor fProgramDesc;
  499. uint32_t fMidiEventCount;
  500. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  501. // -------------------------------------------------------------------
  502. #define handlePtr ((NativePlugin*)handle)
  503. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  504. {
  505. return handlePtr->fBufferSize;
  506. }
  507. static double host_get_sample_rate(NativeHostHandle handle)
  508. {
  509. return handlePtr->fSampleRate;
  510. }
  511. static bool host_is_offline(NativeHostHandle handle)
  512. {
  513. return handlePtr->fIsOffline;
  514. }
  515. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  516. {
  517. return &(handlePtr->fTimeInfo);
  518. }
  519. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  520. {
  521. return handlePtr->handleWriteMidiEvent(event);
  522. }
  523. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  524. {
  525. handlePtr->handleUiParameterChanged(index, value);
  526. }
  527. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  528. {
  529. handlePtr->handleUiCustomDataChanged(key, value);
  530. }
  531. static void host_ui_closed(NativeHostHandle handle)
  532. {
  533. handlePtr->handleUiClosed();
  534. }
  535. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  536. {
  537. return handlePtr->handleUiOpenFile(isDir, title, filter);
  538. }
  539. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  540. {
  541. return handlePtr->handleUiSaveFile(isDir, title, filter);
  542. }
  543. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  544. {
  545. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  546. }
  547. #undef handlePtr
  548. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  549. };
  550. // -----------------------------------------------------------------------
  551. // LV2 plugin descriptor functions
  552. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  553. {
  554. carla_debug("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  555. const NativePluginDescriptor* pluginDesc = nullptr;
  556. const char* pluginLabel = nullptr;
  557. if (std::strncmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla/plugins/", 37) == 0)
  558. pluginLabel = lv2Descriptor->URI+37;
  559. if (pluginLabel == nullptr)
  560. {
  561. carla_stderr("Failed to find carla native plugin with URI \"%s\"", lv2Descriptor->URI);
  562. return nullptr;
  563. }
  564. carla_debug("lv2_instantiate() - looking up label \"%s\"", pluginLabel);
  565. PluginListManager& plm(PluginListManager::getInstance());
  566. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
  567. {
  568. const NativePluginDescriptor* const& tmpDesc(it.getValue(nullptr));
  569. CARLA_SAFE_ASSERT_CONTINUE(tmpDesc != nullptr);
  570. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  571. {
  572. pluginDesc = tmpDesc;
  573. break;
  574. }
  575. }
  576. if (pluginDesc == nullptr)
  577. {
  578. carla_stderr("Failed to find carla native plugin with label \"%s\"", pluginLabel);
  579. return nullptr;
  580. }
  581. NativePlugin* const plugin(new NativePlugin(pluginDesc, sampleRate, bundlePath, features));
  582. if (! plugin->init())
  583. {
  584. carla_stderr("Failed to init plugin");
  585. delete plugin;
  586. return nullptr;
  587. }
  588. return (LV2_Handle)plugin;
  589. }
  590. #define instancePtr ((NativePlugin*)instance)
  591. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  592. {
  593. instancePtr->lv2_connect_port(port, dataLocation);
  594. }
  595. static void lv2_activate(LV2_Handle instance)
  596. {
  597. carla_debug("lv2_activate(%p)", instance);
  598. instancePtr->lv2_activate();
  599. }
  600. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  601. {
  602. instancePtr->lv2_run(sampleCount);
  603. }
  604. static void lv2_deactivate(LV2_Handle instance)
  605. {
  606. carla_debug("lv2_deactivate(%p)", instance);
  607. instancePtr->lv2_deactivate();
  608. }
  609. static void lv2_cleanup(LV2_Handle instance)
  610. {
  611. carla_debug("lv2_cleanup(%p)", instance);
  612. instancePtr->lv2_cleanup();
  613. delete instancePtr;
  614. }
  615. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  616. {
  617. carla_debug("lv2_get_options(%p, %p)", instance, options);
  618. return instancePtr->lv2_get_options(options);
  619. }
  620. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  621. {
  622. carla_debug("lv2_set_options(%p, %p)", instance, options);
  623. return instancePtr->lv2_set_options(options);
  624. }
  625. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  626. {
  627. carla_debug("lv2_get_program(%p, %i)", instance, index);
  628. return instancePtr->lv2_get_program(index);
  629. }
  630. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  631. {
  632. carla_debug("lv2_select_program(%p, %i, %i)", instance, bank, program);
  633. return instancePtr->lv2_select_program(bank, program);
  634. }
  635. 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)
  636. {
  637. carla_debug("lv2_save(%p, %p, %p, %i, %p)", instance, store, handle, flags, features);
  638. return instancePtr->lv2_save(store, handle, flags, features);
  639. }
  640. 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)
  641. {
  642. carla_debug("lv2_restore(%p, %p, %p, %i, %p)", instance, retrieve, handle, flags, features);
  643. return instancePtr->lv2_restore(retrieve, handle, flags, features);
  644. }
  645. static const void* lv2_extension_data(const char* uri)
  646. {
  647. carla_debug("lv2_extension_data(\"%s\")", uri);
  648. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  649. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  650. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  651. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  652. return &options;
  653. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  654. return &programs;
  655. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  656. return &state;
  657. return nullptr;
  658. }
  659. #undef instancePtr
  660. // -----------------------------------------------------------------------
  661. // LV2 UI descriptor functions
  662. static LV2UI_Handle lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  663. LV2UI_Widget* widget, const LV2_Feature* const* features, const bool isEmbed)
  664. {
  665. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  666. #ifndef CARLA_OS_LINUX
  667. CARLA_SAFE_ASSERT_RETURN(! isEmbed, nullptr);
  668. #endif
  669. NativePlugin* plugin = nullptr;
  670. for (int i=0; features[i] != nullptr; ++i)
  671. {
  672. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  673. {
  674. plugin = (NativePlugin*)features[i]->data;
  675. break;
  676. }
  677. }
  678. if (plugin == nullptr)
  679. {
  680. carla_stderr("Host doesn't support instance-access, cannot show UI");
  681. return nullptr;
  682. }
  683. plugin->lv2ui_instantiate(writeFunction, controller, widget, features, isEmbed);
  684. return (LV2UI_Handle)plugin;
  685. }
  686. #ifdef CARLA_OS_LINUX
  687. static LV2UI_Handle lv2ui_instantiate_embed(const LV2UI_Descriptor*, const char*, const char*,
  688. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  689. LV2UI_Widget* widget, const LV2_Feature* const* features)
  690. {
  691. return lv2ui_instantiate(writeFunction, controller, widget, features, true);
  692. }
  693. #endif
  694. static LV2UI_Handle lv2ui_instantiate_external(const LV2UI_Descriptor*, const char*, const char*,
  695. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  696. LV2UI_Widget* widget, const LV2_Feature* const* features)
  697. {
  698. return lv2ui_instantiate(writeFunction, controller, widget, features, false);
  699. }
  700. #define uiPtr ((NativePlugin*)ui)
  701. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  702. {
  703. carla_debug("lv2ui_port_event(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  704. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  705. }
  706. static void lv2ui_cleanup(LV2UI_Handle ui)
  707. {
  708. carla_debug("lv2ui_cleanup(%p)", ui);
  709. uiPtr->lv2ui_cleanup();
  710. }
  711. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  712. {
  713. carla_debug("lv2ui_select_program(%p, %i, %i)", ui, bank, program);
  714. uiPtr->lv2ui_select_program(bank, program);
  715. }
  716. static int lv2ui_idle(LV2UI_Handle ui)
  717. {
  718. return uiPtr->lv2ui_idle();
  719. }
  720. static int lv2ui_show(LV2UI_Handle ui)
  721. {
  722. carla_debug("lv2ui_show(%p)", ui);
  723. return uiPtr->lv2ui_show();
  724. }
  725. static int lv2ui_hide(LV2UI_Handle ui)
  726. {
  727. carla_debug("lv2ui_hide(%p)", ui);
  728. return uiPtr->lv2ui_hide();
  729. }
  730. static const void* lv2ui_extension_data(const char* uri)
  731. {
  732. carla_stdout("lv2ui_extension_data(\"%s\")", uri);
  733. static const LV2UI_Idle_Interface uiidle = { lv2ui_idle };
  734. static const LV2UI_Show_Interface uishow = { lv2ui_show, lv2ui_hide };
  735. static const LV2_Programs_UI_Interface uiprograms = { lv2ui_select_program };
  736. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  737. return &uiidle;
  738. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  739. return &uishow;
  740. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  741. return &uiprograms;
  742. return nullptr;
  743. }
  744. #undef uiPtr
  745. // -----------------------------------------------------------------------
  746. // Startup code
  747. CARLA_EXPORT
  748. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  749. {
  750. carla_debug("lv2_descriptor(%i)", index);
  751. PluginListManager& plm(PluginListManager::getInstance());
  752. if (index >= plm.descs.count())
  753. {
  754. carla_debug("lv2_descriptor(%i) - out of bounds", index);
  755. return nullptr;
  756. }
  757. if (index < plm.lv2Descs.count())
  758. {
  759. carla_debug("lv2_descriptor(%i) - found previously allocated", index);
  760. return plm.lv2Descs.getAt(index, nullptr);
  761. }
  762. const NativePluginDescriptor* const pluginDesc(plm.descs.getAt(index, nullptr));
  763. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, nullptr);
  764. CarlaString tmpURI;
  765. tmpURI = "http://kxstudio.sf.net/carla/plugins/";
  766. tmpURI += pluginDesc->label;
  767. carla_debug("lv2_descriptor(%i) - not found, allocating new with uri \"%s\"", index, (const char*)tmpURI);
  768. const LV2_Descriptor lv2DescTmp = {
  769. /* URI */ carla_strdup(tmpURI),
  770. /* instantiate */ lv2_instantiate,
  771. /* connect_port */ lv2_connect_port,
  772. /* activate */ lv2_activate,
  773. /* run */ lv2_run,
  774. /* deactivate */ lv2_deactivate,
  775. /* cleanup */ lv2_cleanup,
  776. /* extension_data */ lv2_extension_data
  777. };
  778. LV2_Descriptor* lv2Desc;
  779. try {
  780. lv2Desc = new LV2_Descriptor;
  781. } CARLA_SAFE_EXCEPTION_RETURN("new LV2_Descriptor", nullptr);
  782. std::memcpy(lv2Desc, &lv2DescTmp, sizeof(LV2_Descriptor));
  783. plm.lv2Descs.append(lv2Desc);
  784. return lv2Desc;
  785. }
  786. CARLA_EXPORT
  787. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  788. {
  789. carla_debug("lv2ui_descriptor(%i)", index);
  790. #ifdef CARLA_OS_LINUX
  791. static const LV2UI_Descriptor lv2UiEmbedDesc = {
  792. /* URI */ "http://kxstudio.sf.net/carla/ui-embed",
  793. /* instantiate */ lv2ui_instantiate_embed,
  794. /* cleanup */ lv2ui_cleanup,
  795. /* port_event */ lv2ui_port_event,
  796. /* extension_data */ lv2ui_extension_data
  797. };
  798. if (index == 0)
  799. return &lv2UiEmbedDesc;
  800. else
  801. --index;
  802. #endif
  803. static const LV2UI_Descriptor lv2UiExtDesc = {
  804. /* URI */ "http://kxstudio.sf.net/carla/ui-ext",
  805. /* instantiate */ lv2ui_instantiate_external,
  806. /* cleanup */ lv2ui_cleanup,
  807. /* port_event */ lv2ui_port_event,
  808. /* extension_data */ lv2ui_extension_data
  809. };
  810. return (index == 0) ? &lv2UiExtDesc : nullptr;
  811. }
  812. // -----------------------------------------------------------------------