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.

1488 lines
50KB

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