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.

1486 lines
51KB

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