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.

1556 lines
53KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2020 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 "CarlaPipeUtils.hpp"
  22. #include "CarlaString.hpp"
  23. #include "water/files/File.h"
  24. static const char* const kPathForCarlaFiles = "carlafiles";
  25. template<>
  26. void Lv2PluginBaseClass<NativeTimeInfo>::clearTimeData() noexcept
  27. {
  28. fLastPositionData.clear();
  29. carla_zeroStruct(fTimeInfo);
  30. }
  31. struct PreviewData {
  32. char type;
  33. uint32_t size;
  34. const void* buffer;
  35. bool shouldSend;
  36. PreviewData()
  37. : type(0),
  38. size(0),
  39. buffer(nullptr),
  40. shouldSend(false) {}
  41. };
  42. // --------------------------------------------------------------------------------------------------------------------
  43. // Carla Internal Plugin API exposed as LV2 plugin
  44. class NativePlugin : public Lv2PluginBaseClass<NativeTimeInfo>
  45. {
  46. public:
  47. static const uint32_t kMaxMidiEvents = 512;
  48. NativePlugin(const NativePluginDescriptor* const desc,
  49. const double sampleRate,
  50. const char* const bundlePath,
  51. const LV2_Feature* const* const features)
  52. : Lv2PluginBaseClass<NativeTimeInfo>(sampleRate, features),
  53. fHandle(nullptr),
  54. fHost(),
  55. fDescriptor(desc),
  56. #ifdef CARLA_PROPER_CPP11_SUPPORT
  57. fProgramDesc({0, 0, nullptr}),
  58. #endif
  59. kIgnoreParameters(std::strncmp(desc->label, "carla", 5) == 0),
  60. fAtomForge(),
  61. fMidiEventCount(0),
  62. fLastProjectPath(),
  63. fLoadedFile(),
  64. fPreviewData(),
  65. fNeedsNotifyFileChanged(false),
  66. fPluginNeedsIdle(0),
  67. fWorkerUISignal(0)
  68. {
  69. carla_zeroStruct(fHost);
  70. #ifndef CARLA_PROPER_CPP11_SUPPORT
  71. carla_zeroStruct(fProgramDesc);
  72. #endif
  73. if (! loadedInProperHost())
  74. return;
  75. using water::File;
  76. using water::String;
  77. String resourceDir(water::File(bundlePath).getChildFile("resources").getFullPathName());
  78. fHost.handle = this;
  79. fHost.resourceDir = carla_strdup(resourceDir.toRawUTF8());
  80. fHost.uiName = nullptr;
  81. fHost.uiParentId = 0;
  82. fHost.get_buffer_size = host_get_buffer_size;
  83. fHost.get_sample_rate = host_get_sample_rate;
  84. fHost.is_offline = host_is_offline;
  85. fHost.get_time_info = host_get_time_info;
  86. fHost.write_midi_event = host_write_midi_event;
  87. fHost.ui_parameter_changed = host_ui_parameter_changed;
  88. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  89. fHost.ui_closed = host_ui_closed;
  90. fHost.ui_open_file = host_ui_open_file;
  91. fHost.ui_save_file = host_ui_save_file;
  92. fHost.dispatcher = host_dispatcher;
  93. carla_zeroStruct(fAtomForge);
  94. lv2_atom_forge_init(&fAtomForge, fUridMap);
  95. }
  96. ~NativePlugin()
  97. {
  98. CARLA_SAFE_ASSERT(fHandle == nullptr);
  99. if (fHost.resourceDir != nullptr)
  100. {
  101. delete[] fHost.resourceDir;
  102. fHost.resourceDir = nullptr;
  103. }
  104. if (fHost.uiName != nullptr)
  105. {
  106. delete[] fHost.uiName;
  107. fHost.uiName = nullptr;
  108. }
  109. }
  110. // ----------------------------------------------------------------------------------------------------------------
  111. bool init()
  112. {
  113. if (fHost.resourceDir == nullptr)
  114. return false;
  115. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  116. {
  117. carla_stderr("Plugin is missing something...");
  118. return false;
  119. }
  120. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  121. fHandle = fDescriptor->instantiate(&fHost);
  122. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  123. fPorts.hasUI = fDescriptor->hints & NATIVE_PLUGIN_HAS_UI;
  124. fPorts.usesTime = fDescriptor->hints & NATIVE_PLUGIN_USES_TIME;
  125. fPorts.numAudioIns = fDescriptor->audioIns;
  126. fPorts.numAudioOuts = fDescriptor->audioOuts;
  127. fPorts.numCVIns = fDescriptor->cvIns;
  128. fPorts.numCVOuts = fDescriptor->cvOuts;
  129. fPorts.numMidiIns = fDescriptor->midiIns;
  130. fPorts.numMidiOuts = fDescriptor->midiOuts;
  131. if (fDescriptor->get_parameter_count != nullptr &&
  132. fDescriptor->get_parameter_info != nullptr &&
  133. fDescriptor->get_parameter_value != nullptr &&
  134. fDescriptor->set_parameter_value != nullptr &&
  135. ! kIgnoreParameters)
  136. {
  137. fPorts.numParams = fDescriptor->get_parameter_count(fHandle);
  138. }
  139. fPorts.init();
  140. if (fPorts.numParams > 0)
  141. {
  142. for (uint32_t i=0; i < fPorts.numParams; ++i)
  143. {
  144. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  145. fPorts.paramsOut [i] = fDescriptor->get_parameter_info(fHandle, i)->hints & NATIVE_PARAMETER_IS_OUTPUT;
  146. }
  147. }
  148. return true;
  149. }
  150. // ----------------------------------------------------------------------------------------------------------------
  151. // LV2 functions
  152. void lv2_activate()
  153. {
  154. CARLA_SAFE_ASSERT_RETURN(! fIsActive,);
  155. resetTimeInfo();
  156. if (fDescriptor->activate != nullptr)
  157. fDescriptor->activate(fHandle);
  158. fIsActive = true;
  159. }
  160. void lv2_deactivate()
  161. {
  162. CARLA_SAFE_ASSERT_RETURN(fIsActive,);
  163. fIsActive = false;
  164. if (fDescriptor->deactivate != nullptr)
  165. fDescriptor->deactivate(fHandle);
  166. }
  167. void lv2_cleanup()
  168. {
  169. if (fIsActive)
  170. {
  171. carla_stderr("Warning: Host forgot to call deactivate!");
  172. fIsActive = false;
  173. if (fDescriptor->deactivate != nullptr)
  174. fDescriptor->deactivate(fHandle);
  175. }
  176. if (fDescriptor->cleanup != nullptr)
  177. fDescriptor->cleanup(fHandle);
  178. fHandle = nullptr;
  179. }
  180. // ----------------------------------------------------------------------------------------------------------------
  181. void lv2_run(const uint32_t frames)
  182. {
  183. if (! lv2_pre_run(frames))
  184. {
  185. updateParameterOutputs();
  186. return;
  187. }
  188. if (fPorts.numMidiIns > 0 || fPorts.hasUI)
  189. {
  190. uint32_t numEventsIn;
  191. if (fPorts.numMidiIns > 0)
  192. {
  193. numEventsIn = fPorts.numMidiIns;
  194. fMidiEventCount = 0;
  195. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  196. }
  197. else
  198. {
  199. numEventsIn = 1;
  200. }
  201. for (uint32_t i=0; i < numEventsIn; ++i)
  202. {
  203. const LV2_Atom_Sequence* const eventsIn(fPorts.eventsIn[i]);
  204. CARLA_SAFE_ASSERT_CONTINUE(eventsIn != nullptr);
  205. LV2_ATOM_SEQUENCE_FOREACH(eventsIn, event)
  206. {
  207. if (event == nullptr)
  208. continue;
  209. if (event->body.type == fURIs.carlaUiEvents && fWorkerUISignal != -1)
  210. {
  211. CARLA_SAFE_ASSERT_CONTINUE((fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE) == 0);
  212. if (fWorker != nullptr)
  213. {
  214. // worker is supported by the host, we can continue
  215. fWorkerUISignal = 1;
  216. const char* const msg((const char*)(event + 1));
  217. const size_t msgSize = std::strlen(msg);
  218. fWorker->schedule_work(fWorker->handle, static_cast<uint32_t>(msgSize + 1U), msg);
  219. }
  220. else
  221. {
  222. // worker is not supported, cancel
  223. fWorkerUISignal = -1;
  224. }
  225. continue;
  226. }
  227. if (event->body.type == fURIs.atomObject)
  228. {
  229. const LV2_Atom_Object* const obj = (const LV2_Atom_Object*)(&event->body);
  230. if (obj->body.otype == fURIs.patchSet)
  231. {
  232. // Get property URI.
  233. const LV2_Atom* property = nullptr;
  234. lv2_atom_object_get(obj, fURIs.patchProperty, &property, 0);
  235. CARLA_SAFE_ASSERT_CONTINUE(property != nullptr);
  236. CARLA_SAFE_ASSERT_CONTINUE(property->type == fURIs.atomURID);
  237. const LV2_URID urid = ((const LV2_Atom_URID*)property)->body;
  238. /* */ if (std::strcmp(fDescriptor->label, "audiofile") == 0) {
  239. CARLA_SAFE_ASSERT_CONTINUE(urid == fURIs.carlaFileAudio);
  240. } else if (std::strcmp(fDescriptor->label, "midifile") == 0) {
  241. CARLA_SAFE_ASSERT_CONTINUE(urid == fURIs.carlaFileMIDI);
  242. } else {
  243. CARLA_SAFE_ASSERT_CONTINUE(urid == fURIs.carlaFile);
  244. }
  245. // Get value.
  246. const LV2_Atom* fileobj = nullptr;
  247. lv2_atom_object_get(obj, fURIs.patchValue, &fileobj, 0);
  248. CARLA_SAFE_ASSERT_CONTINUE(fileobj != nullptr);
  249. CARLA_SAFE_ASSERT_CONTINUE(fileobj->type == fURIs.atomPath);
  250. const char* const filepath((const char*)(fileobj + 1));
  251. fWorker->schedule_work(fWorker->handle,
  252. static_cast<uint32_t>(std::strlen(filepath) + 1U),
  253. filepath);
  254. }
  255. else if (obj->body.otype == fURIs.patchGet)
  256. {
  257. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE)
  258. fNeedsNotifyFileChanged = true;
  259. if (fPreviewData.buffer != nullptr)
  260. fPreviewData.shouldSend = true;
  261. }
  262. continue;
  263. }
  264. if (event->body.type != fURIs.midiEvent)
  265. continue;
  266. if (event->body.size > 4)
  267. continue;
  268. if (event->time.frames >= frames)
  269. break;
  270. const uint8_t* const data((const uint8_t*)(event + 1));
  271. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  272. nativeEvent.port = (uint8_t)i;
  273. nativeEvent.size = (uint8_t)event->body.size;
  274. nativeEvent.time = (uint32_t)event->time.frames;
  275. uint32_t j=0;
  276. for (uint32_t size=event->body.size; j<size; ++j)
  277. nativeEvent.data[j] = data[j];
  278. for (; j<4; ++j)
  279. nativeEvent.data[j] = 0;
  280. if (fMidiEventCount >= kMaxMidiEvents)
  281. break;
  282. }
  283. }
  284. if (fNeedsNotifyFileChanged || fPreviewData.shouldSend)
  285. {
  286. uint8_t atomBuf[4096];
  287. LV2_Atom_Forge atomForge = fAtomForge;
  288. lv2_atom_forge_set_buffer(&atomForge, atomBuf, sizeof(atomBuf));
  289. const int numEvents = fNeedsNotifyFileChanged && fPreviewData.shouldSend ? 2 : 1;
  290. if (fNeedsNotifyFileChanged)
  291. {
  292. fNeedsNotifyFileChanged = false;
  293. LV2_Atom_Forge_Frame forgeFrame;
  294. lv2_atom_forge_object(&atomForge, &forgeFrame, 0, fURIs.patchSet);
  295. lv2_atom_forge_key(&atomForge, fURIs.patchProperty);
  296. /* */ if (std::strcmp(fDescriptor->label, "audiofile") == 0) {
  297. lv2_atom_forge_urid(&atomForge, fURIs.carlaFileAudio);
  298. } else if (std::strcmp(fDescriptor->label, "midifile") == 0) {
  299. lv2_atom_forge_urid(&atomForge, fURIs.carlaFileMIDI);
  300. } else {
  301. lv2_atom_forge_urid(&atomForge, fURIs.carlaFile);
  302. }
  303. lv2_atom_forge_key(&atomForge, fURIs.patchValue);
  304. lv2_atom_forge_path(&atomForge,
  305. fLoadedFile.buffer(),
  306. static_cast<uint32_t>(fLoadedFile.length()+1));
  307. lv2_atom_forge_pop(&atomForge, &forgeFrame);
  308. }
  309. if (fPreviewData.shouldSend)
  310. {
  311. const char ptype = fPreviewData.type;
  312. const uint32_t psize = fPreviewData.size;
  313. const void* const pbuffer = fPreviewData.buffer;
  314. fPreviewData.shouldSend = false;
  315. LV2_Atom_Forge_Frame forgeFrame;
  316. lv2_atom_forge_object(&atomForge, &forgeFrame, 0, fURIs.patchSet);
  317. lv2_atom_forge_key(&atomForge, fURIs.patchProperty);
  318. lv2_atom_forge_urid(&atomForge, fURIs.carlaPreview);
  319. lv2_atom_forge_key(&atomForge, fURIs.patchValue);
  320. switch (ptype)
  321. {
  322. case 'f':
  323. lv2_atom_forge_vector(&atomForge, sizeof(float), fURIs.atomFloat, psize, pbuffer);
  324. break;
  325. default:
  326. carla_stderr2("Preview data buffer has wrong type '%c' (and size %u)", ptype, psize);
  327. break;
  328. }
  329. lv2_atom_forge_pop(&atomForge, &forgeFrame);
  330. }
  331. LV2_Atom* atom = (LV2_Atom*)atomBuf;
  332. LV2_Atom_Sequence* const seq = fPorts.eventsOut[0];
  333. Ports::EventsOutData& mData(fPorts.eventsOutData[0]);
  334. for (int i=0; i<numEvents; ++i)
  335. {
  336. if (sizeof(LV2_Atom_Event) + atom->size <= mData.capacity - mData.offset)
  337. {
  338. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, seq) + mData.offset);
  339. aev->time.frames = 0;
  340. aev->body.size = atom->size;
  341. aev->body.type = atom->type;
  342. std::memcpy(LV2_ATOM_BODY(&aev->body), atom + 1, atom->size);
  343. const uint32_t size = lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom_Event) + atom->size));
  344. mData.offset += size;
  345. seq->atom.size += size;
  346. }
  347. atom = (LV2_Atom*)(atomBuf + lv2_atom_total_size(atom));
  348. }
  349. }
  350. }
  351. fDescriptor->process(fHandle, fPorts.audioCVIns, fPorts.audioCVOuts, frames, fMidiEvents, fMidiEventCount);
  352. if (fPluginNeedsIdle == 1)
  353. {
  354. fPluginNeedsIdle = 2;
  355. const char* const msg = "_idle_";
  356. const size_t msgSize = std::strlen(msg);
  357. fWorker->schedule_work(fWorker->handle, static_cast<uint32_t>(msgSize + 1U), msg);
  358. }
  359. if (fWorkerUISignal == -1 && fPorts.hasUI)
  360. {
  361. const char* const msg = "quit";
  362. const size_t msgSize = 5;
  363. LV2_Atom_Sequence* const seq(fPorts.eventsOut[0]);
  364. Ports::EventsOutData& mData(fPorts.eventsOutData[0]);
  365. if (sizeof(LV2_Atom_Event) + msgSize <= mData.capacity - mData.offset)
  366. {
  367. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, seq) + mData.offset);
  368. aev->time.frames = 0;
  369. aev->body.size = msgSize;
  370. aev->body.type = fURIs.carlaUiEvents;
  371. std::memcpy(LV2_ATOM_BODY(&aev->body), msg, msgSize);
  372. const uint32_t size = lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom_Event) + msgSize));
  373. mData.offset += size;
  374. seq->atom.size += size;
  375. fWorkerUISignal = 0;
  376. }
  377. }
  378. lv2_post_run(frames);
  379. updateParameterOutputs();
  380. }
  381. // ----------------------------------------------------------------------------------------------------------------
  382. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  383. {
  384. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  385. return nullptr;
  386. if (fDescriptor->get_midi_program_count == nullptr)
  387. return nullptr;
  388. if (fDescriptor->get_midi_program_info == nullptr)
  389. return nullptr;
  390. if (index >= fDescriptor->get_midi_program_count(fHandle))
  391. return nullptr;
  392. const NativeMidiProgram* const midiProg(fDescriptor->get_midi_program_info(fHandle, index));
  393. if (midiProg == nullptr)
  394. return nullptr;
  395. fProgramDesc.bank = midiProg->bank;
  396. fProgramDesc.program = midiProg->program;
  397. fProgramDesc.name = midiProg->name;
  398. return &fProgramDesc;
  399. }
  400. void lv2_select_program(uint32_t bank, uint32_t program)
  401. {
  402. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  403. return;
  404. if (fDescriptor->set_midi_program == nullptr)
  405. return;
  406. fDescriptor->set_midi_program(fHandle, 0, bank, program);
  407. for (uint32_t i=0; i < fPorts.numParams; ++i)
  408. {
  409. fPorts.paramsLast[i] = fDescriptor->get_parameter_value(fHandle, i);
  410. if (fPorts.paramsPtr[i] != nullptr)
  411. *fPorts.paramsPtr[i] = fPorts.paramsLast[i];
  412. }
  413. }
  414. // ----------------------------------------------------------------------------------------------------------------
  415. void cleanupLastProjectPath()
  416. {
  417. fLastProjectPath.clear();
  418. }
  419. void saveLastProjectPathIfPossible(const LV2_Feature* const* const features)
  420. {
  421. if (features == nullptr)
  422. return cleanupLastProjectPath();
  423. const LV2_State_Free_Path* freePath = nullptr;
  424. const LV2_State_Make_Path* makePath = nullptr;
  425. for (int i=0; features[i] != nullptr; ++i)
  426. {
  427. /**/ if (freePath == nullptr && std::strcmp(features[i]->URI, LV2_STATE__freePath) == 0)
  428. freePath = (const LV2_State_Free_Path*)features[i]->data;
  429. else if (makePath == nullptr && std::strcmp(features[i]->URI, LV2_STATE__makePath) == 0)
  430. makePath = (const LV2_State_Make_Path*)features[i]->data;
  431. }
  432. if (makePath == nullptr || makePath->path == nullptr)
  433. return cleanupLastProjectPath();
  434. if (freePath == nullptr)
  435. freePath = fFreePath;
  436. char* const newpath = makePath->path(makePath->handle, kPathForCarlaFiles);
  437. if (newpath == nullptr)
  438. return cleanupLastProjectPath();
  439. fLastProjectPath = CarlaString(water::File(newpath).getParentDirectory().getFullPathName().toRawUTF8());
  440. if (freePath != nullptr && freePath->free_path != nullptr)
  441. freePath->free_path(freePath->handle, newpath);
  442. #ifndef CARLA_OS_WIN
  443. // this is not safe to call under windows
  444. else
  445. std::free(newpath);
  446. #endif
  447. }
  448. LV2_State_Status lv2_save(const LV2_State_Store_Function store, const LV2_State_Handle handle,
  449. const uint32_t /*flags*/, const LV2_Feature* const* const features)
  450. {
  451. saveLastProjectPathIfPossible(features);
  452. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE)
  453. {
  454. if (fLoadedFile.isEmpty())
  455. return LV2_STATE_SUCCESS;
  456. const LV2_State_Free_Path* freePath = nullptr;
  457. const LV2_State_Map_Path* mapPath = nullptr;
  458. if (features != nullptr)
  459. {
  460. for (int i=0; features[i] != nullptr; ++i)
  461. {
  462. /**/ if (freePath == nullptr && std::strcmp(features[i]->URI, LV2_STATE__freePath) == 0)
  463. freePath = (const LV2_State_Free_Path*)features[i]->data;
  464. else if (mapPath == nullptr && std::strcmp(features[i]->URI, LV2_STATE__mapPath) == 0)
  465. mapPath = (const LV2_State_Map_Path*)features[i]->data;
  466. }
  467. }
  468. if (mapPath == nullptr || mapPath->abstract_path == nullptr)
  469. return LV2_STATE_ERR_NO_FEATURE;
  470. char* path = mapPath->abstract_path(mapPath->handle, fLoadedFile.buffer());
  471. store(handle,
  472. fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/file"),
  473. path,
  474. std::strlen(path)+1,
  475. fURIs.atomPath,
  476. LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  477. if (freePath != nullptr && freePath->free_path != nullptr)
  478. freePath->free_path(freePath->handle, path);
  479. #ifndef CARLA_OS_WIN
  480. // this is not safe to call under windows
  481. else
  482. std::free(path);
  483. #endif
  484. return LV2_STATE_SUCCESS;
  485. }
  486. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->get_state == nullptr)
  487. return LV2_STATE_ERR_UNKNOWN;
  488. if (char* const state = fDescriptor->get_state(fHandle))
  489. {
  490. store(handle,
  491. fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"),
  492. state,
  493. std::strlen(state)+1,
  494. fURIs.atomString,
  495. LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  496. std::free(state);
  497. return LV2_STATE_SUCCESS;
  498. }
  499. return LV2_STATE_ERR_UNKNOWN;
  500. }
  501. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function retrieve,
  502. const LV2_State_Handle handle,
  503. uint32_t flags,
  504. const LV2_Feature* const* const features)
  505. {
  506. saveLastProjectPathIfPossible(features);
  507. size_t size = 0;
  508. uint32_t type = 0;
  509. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE)
  510. {
  511. size = type = 0;
  512. const void* const data = retrieve(handle,
  513. fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/file"),
  514. &size, &type, &flags);
  515. if (size <= 1 || type == 0)
  516. return LV2_STATE_ERR_NO_PROPERTY;
  517. CARLA_SAFE_ASSERT_RETURN(type == fURIs.atomPath, LV2_STATE_ERR_UNKNOWN);
  518. const LV2_State_Free_Path* freePath = nullptr;
  519. const LV2_State_Map_Path* mapPath = nullptr;
  520. if (features != nullptr)
  521. {
  522. for (int i=0; features[i] != nullptr; ++i)
  523. {
  524. /**/ if (freePath == nullptr && std::strcmp(features[i]->URI, LV2_STATE__freePath) == 0)
  525. freePath = (const LV2_State_Free_Path*)features[i]->data;
  526. else if (mapPath == nullptr && std::strcmp(features[i]->URI, LV2_STATE__mapPath) == 0)
  527. mapPath = (const LV2_State_Map_Path*)features[i]->data;
  528. }
  529. }
  530. if (mapPath == nullptr || mapPath->absolute_path == nullptr)
  531. return LV2_STATE_ERR_NO_FEATURE;
  532. const char* const filename = (const char*)data;
  533. char* const absolute_filename = mapPath->absolute_path(mapPath->handle, filename);
  534. fLoadedFile = absolute_filename;
  535. if (freePath != nullptr && freePath->free_path != nullptr)
  536. freePath->free_path(freePath->handle, absolute_filename);
  537. #ifndef CARLA_OS_WIN
  538. // this is not safe to call under windows
  539. else
  540. std::free(absolute_filename);
  541. #endif
  542. fNeedsNotifyFileChanged = true;
  543. fDescriptor->set_custom_data(fHandle, "file", fLoadedFile);
  544. return LV2_STATE_SUCCESS;
  545. }
  546. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0 || fDescriptor->set_state == nullptr)
  547. return LV2_STATE_ERR_UNKNOWN;
  548. size = type = 0;
  549. const void* const data = retrieve(handle,
  550. fUridMap->map(fUridMap->handle, "http://kxstudio.sf.net/ns/carla/chunk"),
  551. &size, &type, &flags);
  552. if (size == 0)
  553. return LV2_STATE_ERR_UNKNOWN;
  554. if (type == 0)
  555. return LV2_STATE_ERR_UNKNOWN;
  556. if (data == nullptr)
  557. return LV2_STATE_ERR_UNKNOWN;
  558. if (type != fURIs.atomString)
  559. return LV2_STATE_ERR_BAD_TYPE;
  560. fDescriptor->set_state(fHandle, (const char*)data);
  561. return LV2_STATE_SUCCESS;
  562. }
  563. // ----------------------------------------------------------------------------------------------------------------
  564. LV2_Worker_Status lv2_work(LV2_Worker_Respond_Function, LV2_Worker_Respond_Handle, uint32_t, const void* data)
  565. {
  566. const char* const msg = (const char*)data;
  567. if (std::strcmp(msg, "_idle_") == 0)
  568. {
  569. if (fDescriptor->hints & NATIVE_PLUGIN_REQUESTS_IDLE)
  570. {
  571. fPluginNeedsIdle = 0;
  572. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_IDLE, 0, 0, nullptr, 0.0f);
  573. return LV2_WORKER_SUCCESS;
  574. }
  575. return LV2_WORKER_ERR_UNKNOWN;
  576. }
  577. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE)
  578. {
  579. fLoadedFile = msg;
  580. fDescriptor->set_custom_data(fHandle, "file", msg);
  581. return LV2_WORKER_SUCCESS;
  582. }
  583. /**/ if (std::strncmp(msg, "control ", 8) == 0)
  584. {
  585. if (fDescriptor->ui_set_parameter_value == nullptr)
  586. return LV2_WORKER_SUCCESS;
  587. if (const char* const msgSplit = std::strstr(msg+8, " "))
  588. {
  589. const char* const msgIndex = msg+8;
  590. CARLA_SAFE_ASSERT_RETURN(msgSplit - msgIndex < 8, LV2_WORKER_ERR_UNKNOWN);
  591. CARLA_SAFE_ASSERT_RETURN(msgSplit[0] != '\0', LV2_WORKER_ERR_UNKNOWN);
  592. char strBufIndex[8];
  593. carla_zeroChars(strBufIndex, 8);
  594. std::strncpy(strBufIndex, msgIndex, static_cast<size_t>(msgSplit - msgIndex));
  595. const int index = std::atoi(msgIndex) - static_cast<int>(fPorts.indexOffset);
  596. CARLA_SAFE_ASSERT_RETURN(index >= 0, LV2_WORKER_ERR_UNKNOWN);
  597. float value;
  598. {
  599. const CarlaScopedLocale csl;
  600. value = static_cast<float>(std::atof(msgSplit+1));
  601. }
  602. fDescriptor->ui_set_parameter_value(fHandle, static_cast<uint32_t>(index), value);
  603. }
  604. }
  605. else if (std::strcmp(msg, "show") == 0)
  606. {
  607. handleUiShow();
  608. }
  609. else if (std::strcmp(msg, "hide") == 0)
  610. {
  611. handleUiHide();
  612. }
  613. else if (std::strcmp(msg, "idle") == 0)
  614. {
  615. handleUiRun();
  616. }
  617. else if (std::strcmp(msg, "quit") == 0)
  618. {
  619. handleUiClosed();
  620. }
  621. else
  622. {
  623. carla_stdout("lv2_work unknown msg '%s'", msg);
  624. return LV2_WORKER_ERR_UNKNOWN;
  625. }
  626. return LV2_WORKER_SUCCESS;
  627. }
  628. LV2_Worker_Status lv2_work_resp(uint32_t /*size*/, const void* /*body*/)
  629. {
  630. return LV2_WORKER_SUCCESS;
  631. }
  632. const LV2_Inline_Display_Image_Surface* lv2_idisp_render(const uint32_t width, const uint32_t height)
  633. {
  634. CARLA_SAFE_ASSERT_RETURN(fDescriptor->hints & NATIVE_PLUGIN_HAS_INLINE_DISPLAY, nullptr);
  635. CARLA_SAFE_ASSERT_RETURN(fDescriptor->render_inline_display, nullptr);
  636. CARLA_SAFE_ASSERT_RETURN(width > 0, nullptr);
  637. CARLA_SAFE_ASSERT_RETURN(height > 0, nullptr);
  638. const NativeInlineDisplayImageSurface* nsur = fDescriptor->render_inline_display(fHandle, width, height);
  639. CARLA_SAFE_ASSERT_RETURN(nsur != nullptr, nullptr);
  640. return (const LV2_Inline_Display_Image_Surface*)(nsur);
  641. }
  642. // ----------------------------------------------------------------------------------------------------------------
  643. void lv2ui_instantiate(LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  644. LV2UI_Widget* widget, const LV2_Feature* const* features)
  645. {
  646. fUI.writeFunction = writeFunction;
  647. fUI.controller = controller;
  648. if (fHost.uiName != nullptr)
  649. {
  650. delete[] fHost.uiName;
  651. fHost.uiName = nullptr;
  652. }
  653. // ---------------------------------------------------------------
  654. // see if the host supports external-ui
  655. for (int i=0; features[i] != nullptr; ++i)
  656. {
  657. if (std::strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host) == 0 ||
  658. std::strcmp(features[i]->URI, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  659. {
  660. fUI.host = (const LV2_External_UI_Host*)features[i]->data;
  661. }
  662. if (std::strcmp(features[i]->URI, LV2_UI__touch) == 0)
  663. {
  664. fUI.touch = (const LV2UI_Touch*)features[i]->data;
  665. }
  666. }
  667. if (fUI.host != nullptr)
  668. {
  669. fHost.uiName = carla_strdup(fUI.host->plugin_human_id);
  670. *widget = (LV2_External_UI_Widget_Compat*)this;
  671. return;
  672. }
  673. // ---------------------------------------------------------------
  674. // no external-ui support, use showInterface
  675. for (int i=0; features[i] != nullptr; ++i)
  676. {
  677. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) != 0)
  678. continue;
  679. const LV2_Options_Option* const options((const LV2_Options_Option*)features[i]->data);
  680. CARLA_SAFE_ASSERT_BREAK(options != nullptr);
  681. for (int j=0; options[j].key != 0; ++j)
  682. {
  683. if (options[j].key != fUridMap->map(fUridMap->handle, LV2_UI__windowTitle))
  684. continue;
  685. const char* const title((const char*)options[j].value);
  686. CARLA_SAFE_ASSERT_BREAK(title != nullptr && title[0] != '\0');
  687. fHost.uiName = carla_strdup(title);
  688. break;
  689. }
  690. break;
  691. }
  692. if (fHost.uiName == nullptr)
  693. fHost.uiName = carla_strdup(fDescriptor->name);
  694. *widget = nullptr;
  695. return;
  696. }
  697. void lv2ui_port_event(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer) const
  698. {
  699. if (format != 0 || bufferSize != sizeof(float) || buffer == nullptr)
  700. return;
  701. if (portIndex < fPorts.indexOffset || ! fUI.isVisible)
  702. return;
  703. if (fDescriptor->ui_set_parameter_value == nullptr)
  704. return;
  705. const float value(*(const float*)buffer);
  706. fDescriptor->ui_set_parameter_value(fHandle, portIndex-fPorts.indexOffset, value);
  707. }
  708. // ----------------------------------------------------------------------------------------------------------------
  709. void lv2ui_select_program(uint32_t bank, uint32_t program) const
  710. {
  711. if (fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  712. return;
  713. if (fDescriptor->ui_set_midi_program == nullptr)
  714. return;
  715. fDescriptor->ui_set_midi_program(fHandle, 0, bank, program);
  716. }
  717. // ----------------------------------------------------------------------------------------------------------------
  718. protected:
  719. void handleUiRun() const override
  720. {
  721. if (fDescriptor->ui_idle != nullptr)
  722. fDescriptor->ui_idle(fHandle);
  723. }
  724. void handleUiShow() override
  725. {
  726. if (fDescriptor->ui_show != nullptr)
  727. fDescriptor->ui_show(fHandle, true);
  728. fUI.isVisible = true;
  729. }
  730. void handleUiHide() override
  731. {
  732. if (fDescriptor->ui_show != nullptr)
  733. fDescriptor->ui_show(fHandle, false);
  734. fUI.isVisible = false;
  735. }
  736. // ----------------------------------------------------------------------------------------------------------------
  737. void handleParameterValueChanged(const uint32_t index, const float value) override
  738. {
  739. fDescriptor->set_parameter_value(fHandle, index, value);
  740. }
  741. void handleBufferSizeChanged(const uint32_t bufferSize) override
  742. {
  743. if (fDescriptor->dispatcher == nullptr)
  744. return;
  745. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, bufferSize, nullptr, 0.0f);
  746. }
  747. void handleSampleRateChanged(const double sampleRate) override
  748. {
  749. if (fDescriptor->dispatcher == nullptr)
  750. return;
  751. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)sampleRate);
  752. }
  753. // ----------------------------------------------------------------------------------------------------------------
  754. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  755. {
  756. CARLA_SAFE_ASSERT_RETURN(fPorts.numMidiOuts > 0, false);
  757. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  758. CARLA_SAFE_ASSERT_RETURN(event->size > 0, false);
  759. const uint8_t port(event->port);
  760. CARLA_SAFE_ASSERT_RETURN(port < fPorts.numMidiOuts, false);
  761. LV2_Atom_Sequence* const seq(fPorts.eventsOut[port]);
  762. CARLA_SAFE_ASSERT_RETURN(seq != nullptr, false);
  763. Ports::EventsOutData& mData(fPorts.eventsOutData[port]);
  764. if (sizeof(LV2_Atom_Event) + event->size > mData.capacity - mData.offset)
  765. return false;
  766. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, seq) + mData.offset);
  767. aev->time.frames = event->time;
  768. aev->body.size = event->size;
  769. aev->body.type = fURIs.midiEvent;
  770. std::memcpy(LV2_ATOM_BODY(&aev->body), event->data, event->size);
  771. const uint32_t size = lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom_Event) + event->size));
  772. mData.offset += size;
  773. seq->atom.size += size;
  774. return true;
  775. }
  776. void handleUiParameterChanged(const uint32_t index, const float value) const
  777. {
  778. if (kIgnoreParameters || fWorkerUISignal)
  779. return;
  780. if (fUI.writeFunction != nullptr && fUI.controller != nullptr)
  781. fUI.writeFunction(fUI.controller, index+fPorts.indexOffset, sizeof(float), 0, &value);
  782. }
  783. void handleUiParameterTouch(const uint32_t index, const bool touch) const
  784. {
  785. if (kIgnoreParameters)
  786. return;
  787. if (fUI.touch != nullptr && fUI.touch->touch != nullptr)
  788. fUI.touch->touch(fUI.touch->handle, index+fPorts.indexOffset, touch);
  789. }
  790. void handleUiResize(const uint32_t, const uint32_t) const
  791. {
  792. // nothing here
  793. }
  794. void handlePreviewBufferData(const char type, const uint32_t size, const void* const buffer) noexcept
  795. {
  796. fPreviewData.type = type;
  797. fPreviewData.size = size;
  798. fPreviewData.buffer = buffer;
  799. fPreviewData.shouldSend = true;
  800. }
  801. void handleUiCustomDataChanged(const char* const key, const char* const value) const
  802. {
  803. carla_stdout("TODO: handleUiCustomDataChanged %s %s", key, value);
  804. //storeCustomData(key, value);
  805. if (fUI.writeFunction == nullptr || fUI.controller == nullptr)
  806. return;
  807. }
  808. void handleUiClosed()
  809. {
  810. fUI.isVisible = false;
  811. if (fWorkerUISignal)
  812. fWorkerUISignal = -1;
  813. if (fUI.host != nullptr && fUI.host->ui_closed != nullptr && fUI.controller != nullptr)
  814. fUI.host->ui_closed(fUI.controller);
  815. fUI.host = nullptr;
  816. fUI.touch = nullptr;
  817. fUI.writeFunction = nullptr;
  818. fUI.controller = nullptr;
  819. }
  820. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  821. {
  822. // TODO
  823. return nullptr;
  824. }
  825. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  826. {
  827. // TODO
  828. return nullptr;
  829. }
  830. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  831. {
  832. #ifdef DEBUG
  833. if (opcode != NATIVE_HOST_OPCODE_GET_FILE_PATH)
  834. {
  835. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)",
  836. opcode, index, value, ptr, static_cast<double>(opt));
  837. }
  838. #endif
  839. intptr_t ret = 0;
  840. switch (opcode)
  841. {
  842. case NATIVE_HOST_OPCODE_NULL:
  843. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  844. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  845. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  846. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  847. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  848. case NATIVE_HOST_OPCODE_HOST_IDLE:
  849. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  850. // nothing
  851. break;
  852. case NATIVE_HOST_OPCODE_REQUEST_IDLE:
  853. CARLA_SAFE_ASSERT_RETURN(fDescriptor->hints & NATIVE_PLUGIN_REQUESTS_IDLE, 0);
  854. if (fWorker != nullptr && fPluginNeedsIdle == 0)
  855. {
  856. fPluginNeedsIdle = 1;
  857. return 1;
  858. }
  859. return 0;
  860. case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY:
  861. if (fInlineDisplay != nullptr && fInlineDisplay->queue_draw != nullptr)
  862. {
  863. fInlineDisplay->queue_draw(fInlineDisplay->handle);
  864. return 1;
  865. }
  866. return 0;
  867. case NATIVE_HOST_OPCODE_GET_FILE_PATH:
  868. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  869. if (std::strcmp((char*)ptr, "carla") == 0 && fLastProjectPath != nullptr)
  870. return static_cast<intptr_t>((uintptr_t)fLastProjectPath.buffer());
  871. break;
  872. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  873. handleUiClosed();
  874. break;
  875. case NATIVE_HOST_OPCODE_UI_TOUCH_PARAMETER:
  876. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  877. handleUiParameterTouch(static_cast<uint32_t>(index), value != 0);
  878. break;
  879. case NATIVE_HOST_OPCODE_UI_RESIZE:
  880. CARLA_SAFE_ASSERT_RETURN(index > 0, 0);
  881. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  882. handleUiResize(static_cast<uint32_t>(index), static_cast<uint32_t>(value));
  883. break;
  884. case NATIVE_HOST_OPCODE_PREVIEW_BUFFER_DATA:
  885. CARLA_SAFE_ASSERT_RETURN(index != 0, 0);
  886. CARLA_SAFE_ASSERT_RETURN(index >= 'a', 0);
  887. CARLA_SAFE_ASSERT_RETURN(index <= 'z', 0);
  888. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  889. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  890. handlePreviewBufferData(static_cast<char>(index), static_cast<uint32_t>(value), (const void*)ptr);
  891. break;
  892. }
  893. return ret;
  894. // unused for now
  895. (void)opt;
  896. }
  897. void updateParameterOutputs()
  898. {
  899. float value;
  900. for (uint32_t i=0; i < fPorts.numParams; ++i)
  901. {
  902. if (! fPorts.paramsOut[i])
  903. continue;
  904. fPorts.paramsLast[i] = value = fDescriptor->get_parameter_value(fHandle, i);
  905. if (fPorts.paramsPtr[i] != nullptr)
  906. *fPorts.paramsPtr[i] = value;
  907. }
  908. }
  909. // -------------------------------------------------------------------
  910. private:
  911. // Native data
  912. NativePluginHandle fHandle;
  913. NativeHostDescriptor fHost;
  914. const NativePluginDescriptor* const fDescriptor;
  915. LV2_Program_Descriptor fProgramDesc;
  916. // carla as plugin does not implement lv2 parameter API yet, needed for feedback
  917. const bool kIgnoreParameters;
  918. LV2_Atom_Forge fAtomForge;
  919. uint32_t fMidiEventCount;
  920. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  921. CarlaString fLastProjectPath;
  922. CarlaString fLoadedFile;
  923. PreviewData fPreviewData;
  924. volatile bool fNeedsNotifyFileChanged;
  925. volatile int fPluginNeedsIdle;
  926. int fWorkerUISignal;
  927. // -1 needs close, 0 idle, 1 stuff is writing??
  928. // -------------------------------------------------------------------
  929. #define handlePtr ((NativePlugin*)handle)
  930. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  931. {
  932. return handlePtr->fBufferSize;
  933. }
  934. static double host_get_sample_rate(NativeHostHandle handle)
  935. {
  936. return handlePtr->fSampleRate;
  937. }
  938. static bool host_is_offline(NativeHostHandle handle)
  939. {
  940. return handlePtr->fIsOffline;
  941. }
  942. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  943. {
  944. return &(handlePtr->fTimeInfo);
  945. }
  946. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  947. {
  948. return handlePtr->handleWriteMidiEvent(event);
  949. }
  950. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  951. {
  952. handlePtr->handleUiParameterChanged(index, value);
  953. }
  954. static void host_ui_parameter_touch(NativeHostHandle handle, uint32_t index, bool touch)
  955. {
  956. handlePtr->handleUiParameterTouch(index, touch);
  957. }
  958. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  959. {
  960. handlePtr->handleUiCustomDataChanged(key, value);
  961. }
  962. static void host_ui_closed(NativeHostHandle handle)
  963. {
  964. handlePtr->handleUiClosed();
  965. }
  966. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  967. {
  968. return handlePtr->handleUiOpenFile(isDir, title, filter);
  969. }
  970. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  971. {
  972. return handlePtr->handleUiSaveFile(isDir, title, filter);
  973. }
  974. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  975. {
  976. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  977. }
  978. #undef handlePtr
  979. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  980. };
  981. // -----------------------------------------------------------------------
  982. // LV2 plugin descriptor functions
  983. static LV2_Handle lv2_instantiate(const LV2_Descriptor* lv2Descriptor, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  984. {
  985. carla_debug("lv2_instantiate(%p, %g, %s, %p)", lv2Descriptor, sampleRate, bundlePath, features);
  986. const NativePluginDescriptor* pluginDesc = nullptr;
  987. const char* pluginLabel = nullptr;
  988. if (std::strncmp(lv2Descriptor->URI, "http://kxstudio.sf.net/carla/plugins/", 37) == 0)
  989. pluginLabel = lv2Descriptor->URI+37;
  990. if (pluginLabel == nullptr)
  991. {
  992. carla_stderr("Failed to find carla native plugin with URI \"%s\"", lv2Descriptor->URI);
  993. return nullptr;
  994. }
  995. carla_debug("lv2_instantiate() - looking up label \"%s\"", pluginLabel);
  996. PluginListManager& plm(PluginListManager::getInstance());
  997. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
  998. {
  999. const NativePluginDescriptor* const& tmpDesc(it.getValue(nullptr));
  1000. CARLA_SAFE_ASSERT_CONTINUE(tmpDesc != nullptr);
  1001. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  1002. {
  1003. pluginDesc = tmpDesc;
  1004. break;
  1005. }
  1006. }
  1007. if (pluginDesc == nullptr)
  1008. {
  1009. carla_stderr("Failed to find carla native plugin with label \"%s\"", pluginLabel);
  1010. return nullptr;
  1011. }
  1012. NativePlugin* const plugin(new NativePlugin(pluginDesc, sampleRate, bundlePath, features));
  1013. if (! plugin->init())
  1014. {
  1015. carla_stderr("Failed to init plugin");
  1016. delete plugin;
  1017. return nullptr;
  1018. }
  1019. return (LV2_Handle)plugin;
  1020. }
  1021. #define instancePtr ((NativePlugin*)instance)
  1022. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  1023. {
  1024. instancePtr->lv2_connect_port(port, dataLocation);
  1025. }
  1026. static void lv2_activate(LV2_Handle instance)
  1027. {
  1028. carla_debug("lv2_activate(%p)", instance);
  1029. instancePtr->lv2_activate();
  1030. }
  1031. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  1032. {
  1033. instancePtr->lv2_run(sampleCount);
  1034. }
  1035. static void lv2_deactivate(LV2_Handle instance)
  1036. {
  1037. carla_debug("lv2_deactivate(%p)", instance);
  1038. instancePtr->lv2_deactivate();
  1039. }
  1040. static void lv2_cleanup(LV2_Handle instance)
  1041. {
  1042. carla_debug("lv2_cleanup(%p)", instance);
  1043. instancePtr->lv2_cleanup();
  1044. delete instancePtr;
  1045. }
  1046. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  1047. {
  1048. carla_debug("lv2_get_options(%p, %p)", instance, options);
  1049. return instancePtr->lv2_get_options(options);
  1050. }
  1051. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  1052. {
  1053. carla_debug("lv2_set_options(%p, %p)", instance, options);
  1054. return instancePtr->lv2_set_options(options);
  1055. }
  1056. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  1057. {
  1058. carla_debug("lv2_get_program(%p, %i)", instance, index);
  1059. return instancePtr->lv2_get_program(index);
  1060. }
  1061. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  1062. {
  1063. carla_debug("lv2_select_program(%p, %i, %i)", instance, bank, program);
  1064. return instancePtr->lv2_select_program(bank, program);
  1065. }
  1066. 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)
  1067. {
  1068. carla_debug("lv2_save(%p, %p, %p, %i, %p)", instance, store, handle, flags, features);
  1069. return instancePtr->lv2_save(store, handle, flags, features);
  1070. }
  1071. 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)
  1072. {
  1073. carla_debug("lv2_restore(%p, %p, %p, %i, %p)", instance, retrieve, handle, flags, features);
  1074. return instancePtr->lv2_restore(retrieve, handle, flags, features);
  1075. }
  1076. static LV2_Worker_Status lv2_work(LV2_Handle instance, LV2_Worker_Respond_Function respond, LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  1077. {
  1078. carla_debug("work(%p, %p, %p, %u, %p)", instance, respond, handle, size, data);
  1079. return instancePtr->lv2_work(respond, handle, size, data);
  1080. }
  1081. static LV2_Worker_Status lv2_work_resp(LV2_Handle instance, uint32_t size, const void* body)
  1082. {
  1083. carla_debug("work_resp(%p, %u, %p)", instance, size, body);
  1084. return instancePtr->lv2_work_resp(size, body);
  1085. }
  1086. static const LV2_Inline_Display_Image_Surface* lv2_idisp_render(LV2_Handle instance, uint32_t w, uint32_t h)
  1087. {
  1088. // carla_debug("lv2_idisp_render(%p, %u, %u)", instance, w, h);
  1089. return instancePtr->lv2_idisp_render(w, h);
  1090. }
  1091. static const void* lv2_extension_data(const char* uri)
  1092. {
  1093. carla_debug("lv2_extension_data(\"%s\")", uri);
  1094. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  1095. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  1096. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  1097. static const LV2_Worker_Interface worker = { lv2_work, lv2_work_resp, nullptr };
  1098. static const LV2_Inline_Display_Interface idisp = { lv2_idisp_render };
  1099. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  1100. return &options;
  1101. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  1102. return &programs;
  1103. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  1104. return &state;
  1105. if (std::strcmp(uri, LV2_WORKER__interface) == 0)
  1106. return &worker;
  1107. if (std::strcmp(uri, LV2_INLINEDISPLAY__interface) == 0)
  1108. return &idisp;
  1109. return nullptr;
  1110. }
  1111. #undef instancePtr
  1112. #ifdef HAVE_PYQT
  1113. // -----------------------------------------------------------------------
  1114. // LV2 UI descriptor functions
  1115. static LV2UI_Handle lv2ui_instantiate(const LV2UI_Descriptor*, const char*, const char*,
  1116. LV2UI_Write_Function writeFunction, LV2UI_Controller controller,
  1117. LV2UI_Widget* widget, const LV2_Feature* const* features)
  1118. {
  1119. carla_debug("lv2ui_instantiate(..., %p, %p, %p)", writeFunction, controller, widget, features);
  1120. NativePlugin* plugin = nullptr;
  1121. for (int i=0; features[i] != nullptr; ++i)
  1122. {
  1123. if (std::strcmp(features[i]->URI, LV2_INSTANCE_ACCESS_URI) == 0)
  1124. {
  1125. plugin = (NativePlugin*)features[i]->data;
  1126. break;
  1127. }
  1128. }
  1129. if (plugin == nullptr)
  1130. {
  1131. carla_stderr("Host doesn't support instance-access, cannot show UI");
  1132. return nullptr;
  1133. }
  1134. plugin->lv2ui_instantiate(writeFunction, controller, widget, features);
  1135. return (LV2UI_Handle)plugin;
  1136. }
  1137. #define uiPtr ((NativePlugin*)ui)
  1138. static void lv2ui_port_event(LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  1139. {
  1140. carla_debug("lv2ui_port_event(%p, %i, %i, %i, %p)", ui, portIndex, bufferSize, format, buffer);
  1141. uiPtr->lv2ui_port_event(portIndex, bufferSize, format, buffer);
  1142. }
  1143. static void lv2ui_cleanup(LV2UI_Handle ui)
  1144. {
  1145. carla_debug("lv2ui_cleanup(%p)", ui);
  1146. uiPtr->lv2ui_cleanup();
  1147. }
  1148. static void lv2ui_select_program(LV2UI_Handle ui, uint32_t bank, uint32_t program)
  1149. {
  1150. carla_debug("lv2ui_select_program(%p, %i, %i)", ui, bank, program);
  1151. uiPtr->lv2ui_select_program(bank, program);
  1152. }
  1153. static int lv2ui_idle(LV2UI_Handle ui)
  1154. {
  1155. return uiPtr->lv2ui_idle();
  1156. }
  1157. static int lv2ui_show(LV2UI_Handle ui)
  1158. {
  1159. carla_debug("lv2ui_show(%p)", ui);
  1160. return uiPtr->lv2ui_show();
  1161. }
  1162. static int lv2ui_hide(LV2UI_Handle ui)
  1163. {
  1164. carla_debug("lv2ui_hide(%p)", ui);
  1165. return uiPtr->lv2ui_hide();
  1166. }
  1167. static const void* lv2ui_extension_data(const char* uri)
  1168. {
  1169. carla_stdout("lv2ui_extension_data(\"%s\")", uri);
  1170. static const LV2UI_Idle_Interface uiidle = { lv2ui_idle };
  1171. static const LV2UI_Show_Interface uishow = { lv2ui_show, lv2ui_hide };
  1172. static const LV2_Programs_UI_Interface uiprograms = { lv2ui_select_program };
  1173. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  1174. return &uiidle;
  1175. if (std::strcmp(uri, LV2_UI__showInterface) == 0)
  1176. return &uishow;
  1177. if (std::strcmp(uri, LV2_PROGRAMS__UIInterface) == 0)
  1178. return &uiprograms;
  1179. return nullptr;
  1180. }
  1181. #endif
  1182. #undef uiPtr
  1183. // -----------------------------------------------------------------------
  1184. // Startup code
  1185. CARLA_EXPORT
  1186. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  1187. {
  1188. carla_debug("lv2_descriptor(%i)", index);
  1189. PluginListManager& plm(PluginListManager::getInstance());
  1190. if (index >= plm.descs.count())
  1191. {
  1192. carla_debug("lv2_descriptor(%i) - out of bounds", index);
  1193. return nullptr;
  1194. }
  1195. if (index < plm.lv2Descs.count())
  1196. {
  1197. carla_debug("lv2_descriptor(%i) - found previously allocated", index);
  1198. return plm.lv2Descs.getAt(index, nullptr);
  1199. }
  1200. const NativePluginDescriptor* const pluginDesc(plm.descs.getAt(index, nullptr));
  1201. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, nullptr);
  1202. CarlaString tmpURI;
  1203. tmpURI = "http://kxstudio.sf.net/carla/plugins/";
  1204. tmpURI += pluginDesc->label;
  1205. carla_debug("lv2_descriptor(%i) - not found, allocating new with uri \"%s\"", index, (const char*)tmpURI);
  1206. const LV2_Descriptor lv2DescTmp = {
  1207. /* URI */ carla_strdup(tmpURI),
  1208. /* instantiate */ lv2_instantiate,
  1209. /* connect_port */ lv2_connect_port,
  1210. /* activate */ lv2_activate,
  1211. /* run */ lv2_run,
  1212. /* deactivate */ lv2_deactivate,
  1213. /* cleanup */ lv2_cleanup,
  1214. /* extension_data */ lv2_extension_data
  1215. };
  1216. LV2_Descriptor* lv2Desc;
  1217. try {
  1218. lv2Desc = new LV2_Descriptor;
  1219. } CARLA_SAFE_EXCEPTION_RETURN("new LV2_Descriptor", nullptr);
  1220. std::memcpy(lv2Desc, &lv2DescTmp, sizeof(LV2_Descriptor));
  1221. plm.lv2Descs.append(lv2Desc);
  1222. return lv2Desc;
  1223. }
  1224. #ifdef HAVE_PYQT
  1225. CARLA_EXPORT
  1226. const LV2UI_Descriptor* lv2ui_descriptor(uint32_t index)
  1227. {
  1228. carla_debug("lv2ui_descriptor(%i)", index);
  1229. static const LV2UI_Descriptor lv2UiExtDesc = {
  1230. /* URI */ "http://kxstudio.sf.net/carla/ui-ext",
  1231. /* instantiate */ lv2ui_instantiate,
  1232. /* cleanup */ lv2ui_cleanup,
  1233. /* port_event */ lv2ui_port_event,
  1234. /* extension_data */ lv2ui_extension_data
  1235. };
  1236. return (index == 0) ? &lv2UiExtDesc : nullptr;
  1237. }
  1238. #endif
  1239. // -----------------------------------------------------------------------