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.

1196 lines
41KB

  1. /*
  2. * Carla Bridge UI, LV2 version
  3. * Copyright (C) 2011-2013 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 GPL.txt file
  16. */
  17. #include "CarlaBridgeClient.hpp"
  18. #include "CarlaLv2Utils.hpp"
  19. #include "CarlaMIDI.h"
  20. #include "RtList.hpp"
  21. #include <QtCore/QDir>
  22. extern "C" {
  23. #include "rtmempool/rtmempool-lv2.h"
  24. }
  25. // -----------------------------------------------------
  26. // Our LV2 World class object
  27. Lv2WorldClass gLv2World;
  28. // -----------------------------------------------------
  29. CARLA_BRIDGE_START_NAMESPACE
  30. #if 0
  31. }
  32. #endif
  33. static uint32_t gBufferSize = 1024;
  34. static double gSampleRate = 44100.0;
  35. // static max values
  36. const unsigned int MAX_EVENT_BUFFER = 8192; // 0x2000
  37. // LV2 URI Map Ids
  38. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  39. const uint32_t CARLA_URI_MAP_ID_ATOM_BLANK = 1;
  40. const uint32_t CARLA_URI_MAP_ID_ATOM_BOOL = 2;
  41. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 3;
  42. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 4;
  43. const uint32_t CARLA_URI_MAP_ID_ATOM_FLOAT = 5;
  44. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 6;
  45. const uint32_t CARLA_URI_MAP_ID_ATOM_LITERAL = 7;
  46. const uint32_t CARLA_URI_MAP_ID_ATOM_LONG = 8;
  47. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 9;
  48. const uint32_t CARLA_URI_MAP_ID_ATOM_PROPERTY = 10;
  49. const uint32_t CARLA_URI_MAP_ID_ATOM_RESOURCE = 11;
  50. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 12;
  51. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 13;
  52. const uint32_t CARLA_URI_MAP_ID_ATOM_TUPLE = 14;
  53. const uint32_t CARLA_URI_MAP_ID_ATOM_URI = 15;
  54. const uint32_t CARLA_URI_MAP_ID_ATOM_URID = 16;
  55. const uint32_t CARLA_URI_MAP_ID_ATOM_VECTOR = 17;
  56. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 18;
  57. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 19;
  58. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 20;
  59. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 21;
  60. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 22;
  61. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 23;
  62. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 24;
  63. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 25;
  64. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 26;
  65. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 27; // base type
  66. const uint32_t CARLA_URI_MAP_ID_TIME_BAR = 28; // values
  67. const uint32_t CARLA_URI_MAP_ID_TIME_BAR_BEAT = 29;
  68. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT = 30;
  69. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT_UNIT = 31;
  70. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR = 32;
  71. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE = 33;
  72. const uint32_t CARLA_URI_MAP_ID_TIME_FRAME = 34;
  73. const uint32_t CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND = 35;
  74. const uint32_t CARLA_URI_MAP_ID_TIME_SPEED = 36;
  75. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 37;
  76. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 38;
  77. const uint32_t CARLA_URI_MAP_ID_COUNT = 39;
  78. // LV2 Feature Ids
  79. const uint32_t kFeatureIdLogs = 0;
  80. const uint32_t kFeatureIdOptions = 1;
  81. const uint32_t kFeatureIdPrograms = 2;
  82. const uint32_t kFeatureIdStateMakePath = 3;
  83. const uint32_t kFeatureIdStateMapPath = 4;
  84. const uint32_t kFeatureIdUriMap = 5;
  85. const uint32_t kFeatureIdUridMap = 6;
  86. const uint32_t kFeatureIdUridUnmap = 7;
  87. const uint32_t kFeatureIdUiIdle = 8;
  88. const uint32_t kFeatureIdUiFixedSize = 9;
  89. const uint32_t kFeatureIdUiMakeResident = 10;
  90. const uint32_t kFeatureIdUiNoUserResize = 11;
  91. const uint32_t kFeatureIdUiParent = 12;
  92. const uint32_t kFeatureIdUiPortMap = 13;
  93. const uint32_t kFeatureIdUiPortSubscribe = 14;
  94. const uint32_t kFeatureIdUiResize = 15;
  95. const uint32_t kFeatureIdUiTouch = 16;
  96. const uint32_t kFeatureCount = 17;
  97. // -------------------------------------------------------------------------
  98. struct Lv2PluginOptions {
  99. int maxBufferSize;
  100. int minBufferSize;
  101. int sequenceSize;
  102. double sampleRate;
  103. LV2_Options_Option optMaxBlockLenth;
  104. LV2_Options_Option optMinBlockLenth;
  105. LV2_Options_Option optSequenceSize;
  106. LV2_Options_Option optSampleRate;
  107. LV2_Options_Option optNull;
  108. LV2_Options_Option* opts[5];
  109. Lv2PluginOptions()
  110. : maxBufferSize(0),
  111. minBufferSize(0),
  112. sequenceSize(MAX_EVENT_BUFFER),
  113. sampleRate(0.0)
  114. {
  115. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  116. optMaxBlockLenth.subject = 0;
  117. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  118. optMaxBlockLenth.size = sizeof(int);
  119. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  120. optMaxBlockLenth.value = &maxBufferSize;
  121. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  122. optMinBlockLenth.subject = 0;
  123. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  124. optMinBlockLenth.size = sizeof(int);
  125. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  126. optMinBlockLenth.value = &minBufferSize;
  127. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  128. optSequenceSize.subject = 0;
  129. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  130. optSequenceSize.size = sizeof(int);
  131. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  132. optSequenceSize.value = &sequenceSize;
  133. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  134. optSampleRate.subject = 0;
  135. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  136. optSampleRate.size = sizeof(double);
  137. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  138. optSampleRate.value = &sampleRate;
  139. optNull.context = LV2_OPTIONS_INSTANCE;
  140. optNull.subject = 0;
  141. optNull.key = CARLA_URI_MAP_ID_NULL;
  142. optNull.size = 0;
  143. optNull.type = CARLA_URI_MAP_ID_NULL;
  144. optNull.value = nullptr;
  145. opts[0] = &optMinBlockLenth;
  146. opts[1] = &optMaxBlockLenth;
  147. opts[2] = &optSequenceSize;
  148. opts[3] = &optSampleRate;
  149. opts[4] = &optNull;
  150. }
  151. CARLA_DECLARE_NON_COPY_STRUCT(Lv2PluginOptions)
  152. };
  153. // -------------------------------------------------------------------------
  154. class CarlaLv2Client : public CarlaBridgeClient
  155. {
  156. public:
  157. CarlaLv2Client(const char* const uiTitle)
  158. : CarlaBridgeClient(uiTitle),
  159. fHandle(nullptr),
  160. fWidget(nullptr),
  161. #ifdef CARLA_PROPER_CPP11_SUPPORT
  162. fFeatures{nullptr},
  163. #endif
  164. fDescriptor(nullptr),
  165. fRdfDescriptor(nullptr),
  166. fRdfUiDescriptor(nullptr),
  167. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  168. fIsResizable(false)
  169. #else
  170. fIsResizable(true)
  171. #endif
  172. {
  173. #ifndef CARLA_PROPER_CPP11_SUPPORT
  174. carla_fill<LV2_Feature*>(fFeatures, kFeatureCount+1, nullptr);
  175. #endif
  176. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  177. fCustomURIDs.append(nullptr);
  178. // ---------------------------------------------------------------
  179. // initialize options
  180. fOptions.minBufferSize = gBufferSize;
  181. fOptions.maxBufferSize = gBufferSize;
  182. fOptions.sampleRate = gSampleRate;
  183. // ---------------------------------------------------------------
  184. // initialize features (part 1)
  185. LV2_Log_Log* const logFt = new LV2_Log_Log;
  186. logFt->handle = this;
  187. logFt->printf = carla_lv2_log_printf;
  188. logFt->vprintf = carla_lv2_log_vprintf;
  189. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  190. stateMakePathFt->handle = this;
  191. stateMakePathFt->path = carla_lv2_state_make_path;
  192. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  193. stateMapPathFt->handle = this;
  194. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  195. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  196. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  197. programsFt->handle = this;
  198. programsFt->program_changed = carla_lv2_program_changed;
  199. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  200. uriMapFt->callback_data = this;
  201. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  202. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  203. uridMapFt->handle = this;
  204. uridMapFt->map = carla_lv2_urid_map;
  205. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  206. uridUnmapFt->handle = this;
  207. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  208. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  209. uiPortMapFt->handle = this;
  210. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  211. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  212. uiResizeFt->handle = this;
  213. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  214. // ---------------------------------------------------------------
  215. // initialize features (part 2)
  216. for (uint32_t i=0; i < kFeatureCount; ++i)
  217. fFeatures[i] = new LV2_Feature;
  218. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  219. fFeatures[kFeatureIdLogs]->data = logFt;
  220. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  221. fFeatures[kFeatureIdOptions]->data = fOptions.opts;
  222. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  223. fFeatures[kFeatureIdPrograms]->data = programsFt;
  224. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  225. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  226. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  227. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  228. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  229. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  230. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  231. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  232. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  233. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  234. fFeatures[kFeatureIdUiIdle]->URI = LV2_UI__idle;
  235. fFeatures[kFeatureIdUiIdle]->data = nullptr;
  236. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  237. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  238. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  239. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  240. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  241. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  242. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  243. fFeatures[kFeatureIdUiParent]->data = nullptr;
  244. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  245. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  246. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  247. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  248. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  249. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  250. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  251. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  252. }
  253. ~CarlaLv2Client() override
  254. {
  255. if (fRdfDescriptor != nullptr)
  256. delete fRdfDescriptor;
  257. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  258. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  259. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  260. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  261. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  262. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  263. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  264. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  265. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  266. for (uint32_t i=0; i < kFeatureCount; ++i)
  267. {
  268. if (fFeatures[i] != nullptr)
  269. {
  270. delete fFeatures[i];
  271. fFeatures[i] = nullptr;
  272. }
  273. }
  274. for (NonRtList<const char*>::Itenerator it = fCustomURIDs.begin(); it.valid(); it.next())
  275. {
  276. const char*& uri(*it);
  277. if (uri != nullptr)
  278. {
  279. delete[] uri;
  280. uri = nullptr;
  281. }
  282. }
  283. fCustomURIDs.clear();
  284. }
  285. // ---------------------------------------------------------------------
  286. // ui initialization
  287. bool uiInit(const char* pluginURI, const char* uiURI) override
  288. {
  289. // -----------------------------------------------------------------
  290. // init
  291. CarlaBridgeClient::uiInit(pluginURI, uiURI);
  292. // -----------------------------------------------------------------
  293. // get plugin from lv2_rdf (lilv)
  294. gLv2World.init();
  295. fRdfDescriptor = lv2_rdf_new(pluginURI, false);
  296. if (fRdfDescriptor == nullptr)
  297. return false;
  298. // -----------------------------------------------------------------
  299. // find requested UI
  300. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  301. {
  302. if (std::strcmp(fRdfDescriptor->UIs[i].URI, uiURI) == 0)
  303. {
  304. fRdfUiDescriptor = &fRdfDescriptor->UIs[i];
  305. break;
  306. }
  307. }
  308. if (fRdfUiDescriptor == nullptr)
  309. return false;
  310. // -----------------------------------------------------------------
  311. // open DLL
  312. if (! uiLibOpen(fRdfUiDescriptor->Binary))
  313. return false;
  314. // -----------------------------------------------------------------
  315. // get DLL main entry
  316. const LV2UI_DescriptorFunction ui_descFn = (LV2UI_DescriptorFunction)uiLibSymbol("lv2ui_descriptor");
  317. if (ui_descFn == nullptr)
  318. return false;
  319. // -----------------------------------------------------------
  320. // get descriptor that matches URI
  321. uint32_t i = 0;
  322. while ((fDescriptor = ui_descFn(i++)))
  323. {
  324. if (std::strcmp(fDescriptor->URI, uiURI) == 0)
  325. break;
  326. }
  327. if (fDescriptor == nullptr)
  328. return false;
  329. // -----------------------------------------------------------
  330. // initialize UI
  331. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  332. fFeatures[kFeatureIdUiParent]->data = getContainerId();
  333. #endif
  334. fHandle = fDescriptor->instantiate(fDescriptor, pluginURI, fRdfUiDescriptor->Bundle, carla_lv2_ui_write_function, this, &fWidget, fFeatures);
  335. if (fHandle == nullptr)
  336. return false;
  337. // -----------------------------------------------------------
  338. // check if not resizable
  339. for (uint32_t i=0; i < fRdfUiDescriptor->FeatureCount && fIsResizable; ++i)
  340. {
  341. if (std::strcmp(fRdfUiDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0 || std::strcmp(fRdfUiDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  342. {
  343. fIsResizable = false;
  344. break;
  345. }
  346. }
  347. // -----------------------------------------------------------
  348. // check for known extensions
  349. if (fDescriptor->extension_data != nullptr)
  350. {
  351. fExt.programs = (const LV2_Programs_UI_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__UIInterface);
  352. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  353. fExt.idle = (const LV2UI_Idle_Interface*)fDescriptor->extension_data(LV2_UI__idleInterface);
  354. // check if invalid
  355. if (fExt.programs != nullptr && fExt.programs->select_program == nullptr)
  356. fExt.programs = nullptr;
  357. if (fExt.idle != nullptr && fExt.idle->idle == nullptr)
  358. fExt.idle = nullptr;
  359. }
  360. return true;
  361. }
  362. void uiIdle() override
  363. {
  364. if (fHandle != nullptr && fExt.idle != nullptr)
  365. fExt.idle->idle(fHandle);
  366. }
  367. void uiClose() override
  368. {
  369. CarlaBridgeClient::uiClose();
  370. if (fHandle && fDescriptor && fDescriptor->cleanup)
  371. fDescriptor->cleanup(fHandle);
  372. uiLibClose();
  373. }
  374. // ---------------------------------------------------------------------
  375. // ui management
  376. void* getWidget() const override
  377. {
  378. return fWidget;
  379. }
  380. bool isResizable() const override
  381. {
  382. return fIsResizable;
  383. }
  384. bool needsReparent() const override
  385. {
  386. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  387. return true;
  388. #else
  389. return false;
  390. #endif
  391. }
  392. // ---------------------------------------------------------------------
  393. // ui processing
  394. void setParameter(const int32_t rindex, const float value) override
  395. {
  396. CARLA_ASSERT(fHandle != nullptr && fDescriptor != nullptr);
  397. if (fHandle != nullptr && fDescriptor != nullptr && fDescriptor->port_event != nullptr)
  398. fDescriptor->port_event(fHandle, rindex, sizeof(float), 0, &value);
  399. }
  400. void setProgram(const uint32_t) override
  401. {
  402. }
  403. void setMidiProgram(const uint32_t bank, const uint32_t program) override
  404. {
  405. CARLA_ASSERT(fHandle != nullptr);
  406. if (fHandle != nullptr && fExt.programs != nullptr)
  407. fExt.programs->select_program(fHandle, bank, program);
  408. }
  409. void noteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  410. {
  411. CARLA_ASSERT(fHandle != nullptr && fDescriptor != nullptr);
  412. if (fHandle != nullptr && fDescriptor != nullptr && fDescriptor->port_event != nullptr)
  413. {
  414. LV2_Atom_MidiEvent midiEv;
  415. midiEv.event.time.frames = 0;
  416. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  417. midiEv.event.body.size = 3;
  418. midiEv.data[0] = MIDI_STATUS_NOTE_ON + channel;
  419. midiEv.data[1] = note;
  420. midiEv.data[2] = velo;
  421. fDescriptor->port_event(fHandle, 0, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  422. }
  423. }
  424. void noteOff(const uint8_t channel, const uint8_t note) override
  425. {
  426. CARLA_ASSERT(fHandle != nullptr && fDescriptor != nullptr);
  427. if (fHandle != nullptr && fDescriptor != nullptr && fDescriptor->port_event != nullptr)
  428. {
  429. LV2_Atom_MidiEvent midiEv;
  430. midiEv.event.time.frames = 0;
  431. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  432. midiEv.event.body.size = 3;
  433. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  434. midiEv.data[1] = note;
  435. midiEv.data[2] = 0;
  436. fDescriptor->port_event(fHandle, 0, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  437. }
  438. }
  439. // ---------------------------------------------------------------------
  440. LV2_URID getCustomURID(const char* const uri)
  441. {
  442. CARLA_ASSERT(uri != nullptr);
  443. carla_debug("CarlaLv2Client::getCustomURID(\"%s\")", uri);
  444. if (uri == nullptr)
  445. return CARLA_URI_MAP_ID_NULL;
  446. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  447. {
  448. const char*& thisUri(fCustomURIDs.getAt(i));
  449. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  450. return i;
  451. }
  452. fCustomURIDs.append(carla_strdup(uri));
  453. const LV2_URID urid(fCustomURIDs.count()-1);
  454. if (isOscControlRegistered())
  455. sendOscLv2UridMap(urid, uri);
  456. return urid;
  457. }
  458. const char* getCustomURIString(const LV2_URID urid)
  459. {
  460. CARLA_ASSERT(urid != CARLA_URI_MAP_ID_NULL);
  461. CARLA_ASSERT_INT2(urid < fCustomURIDs.count(), urid, fCustomURIDs.count());
  462. carla_debug("CarlaLv2Client::getCustomURIString(%i)", urid);
  463. if (urid == CARLA_URI_MAP_ID_NULL)
  464. return nullptr;
  465. if (urid < fCustomURIDs.count())
  466. return fCustomURIDs.getAt(urid);
  467. return nullptr;
  468. }
  469. // ---------------------------------------------------------------------
  470. void handleProgramChanged(const int32_t /*index*/)
  471. {
  472. if (isOscControlRegistered())
  473. sendOscConfigure("reloadprograms", "");
  474. }
  475. uint32_t handleUiPortMap(const char* const symbol)
  476. {
  477. CARLA_ASSERT(symbol != nullptr);
  478. if (symbol == nullptr)
  479. return LV2UI_INVALID_PORT_INDEX;
  480. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  481. {
  482. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  483. return i;
  484. }
  485. return LV2UI_INVALID_PORT_INDEX;
  486. }
  487. int handleUiResize(const int width, const int height)
  488. {
  489. CARLA_ASSERT(width > 0);
  490. CARLA_ASSERT(height > 0);
  491. if (width <= 0 || height <= 0)
  492. return 1;
  493. toolkitResize(width, height);
  494. return 0;
  495. }
  496. void handleUiWrite(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  497. {
  498. if (format == 0)
  499. {
  500. CARLA_ASSERT(buffer != nullptr);
  501. CARLA_ASSERT(bufferSize == sizeof(float));
  502. if (bufferSize != sizeof(float))
  503. return;
  504. if (buffer == nullptr || bufferSize != sizeof(float))
  505. return;
  506. const float value(*(const float*)buffer);
  507. if (isOscControlRegistered())
  508. sendOscControl(portIndex, value);
  509. }
  510. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM || CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  511. {
  512. CARLA_ASSERT(bufferSize != 0);
  513. CARLA_ASSERT(buffer != nullptr);
  514. if (bufferSize == 0 || buffer == nullptr)
  515. return;
  516. if (isOscControlRegistered())
  517. sendOscLv2AtomTransfer(portIndex, QByteArray((const char*)buffer, bufferSize).toBase64().constData());
  518. }
  519. else
  520. {
  521. carla_stdout("CarlaLv2Client::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", portIndex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  522. }
  523. }
  524. // ---------------------------------------------------------------------
  525. void handleAtomTransfer(const uint32_t portIndex, const LV2_Atom* const atom)
  526. {
  527. CARLA_ASSERT(atom != nullptr);
  528. carla_debug("CarlaLv2Client::handleTransferEvent(%i, %p)", portIndex, atom);
  529. if (atom != nullptr && fHandle != nullptr && fDescriptor != nullptr && fDescriptor->port_event != nullptr)
  530. fDescriptor->port_event(fHandle, portIndex, lv2_atom_total_size(atom), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  531. }
  532. void handleUridMap(const LV2_URID urid, const char* const uri)
  533. {
  534. CARLA_ASSERT(urid != CARLA_URI_MAP_ID_NULL);
  535. CARLA_ASSERT(uri != nullptr);
  536. carla_debug("CarlaLv2Client::handleUridMap(%i, \"%s\")", urid, uri);
  537. // TODO
  538. }
  539. private:
  540. LV2UI_Handle fHandle;
  541. LV2UI_Widget fWidget;
  542. LV2_Feature* fFeatures[kFeatureCount+1];
  543. const LV2UI_Descriptor* fDescriptor;
  544. const LV2_RDF_Descriptor* fRdfDescriptor;
  545. const LV2_RDF_UI* fRdfUiDescriptor;
  546. Lv2PluginOptions fOptions;
  547. bool fIsResizable;
  548. NonRtList<const char*> fCustomURIDs;
  549. struct Extensions {
  550. const LV2_Options_Interface* options;
  551. const LV2UI_Idle_Interface* idle;
  552. const LV2_Programs_UI_Interface* programs;
  553. Extensions()
  554. : options(nullptr),
  555. idle(nullptr),
  556. programs(nullptr) {}
  557. } fExt;
  558. // -------------------------------------------------------------------
  559. // Logs Feature
  560. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  561. {
  562. CARLA_ASSERT(handle != nullptr);
  563. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  564. #ifndef DEBUG
  565. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  566. return 0;
  567. #endif
  568. va_list args;
  569. va_start(args, fmt);
  570. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  571. va_end(args);
  572. return ret;
  573. }
  574. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  575. {
  576. CARLA_ASSERT(handle != nullptr);
  577. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  578. #ifndef DEBUG
  579. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  580. return 0;
  581. #endif
  582. int ret = 0;
  583. switch (type)
  584. {
  585. case CARLA_URI_MAP_ID_LOG_ERROR:
  586. #ifndef CARLA_OS_WIN
  587. std::fprintf(stderr, "\x1b[31m");
  588. #endif
  589. ret = std::vfprintf(stderr, fmt, ap);
  590. #ifndef CARLA_OS_WIN
  591. std::fprintf(stderr, "\x1b[0m");
  592. #endif
  593. break;
  594. case CARLA_URI_MAP_ID_LOG_NOTE:
  595. ret = std::vfprintf(stdout, fmt, ap);
  596. break;
  597. case CARLA_URI_MAP_ID_LOG_TRACE:
  598. #ifdef DEBUG
  599. # ifndef CARLA_OS_WIN
  600. std::fprintf(stdout, "\x1b[30;1m");
  601. # endif
  602. ret = std::vfprintf(stdout, fmt, ap);
  603. # ifndef CARLA_OS_WIN
  604. std::fprintf(stdout, "\x1b[0m");
  605. # endif
  606. #endif
  607. break;
  608. case CARLA_URI_MAP_ID_LOG_WARNING:
  609. ret = std::vfprintf(stderr, fmt, ap);
  610. break;
  611. default:
  612. break;
  613. }
  614. return ret;
  615. }
  616. // -------------------------------------------------------------------
  617. // Programs Feature
  618. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  619. {
  620. carla_debug("CarlaLv2Client::carla_lv2_program_changed(%p, %i)", handle, index);
  621. CARLA_ASSERT(handle != nullptr);
  622. if (handle == nullptr)
  623. return;
  624. ((CarlaLv2Client*)handle)->handleProgramChanged(index);
  625. }
  626. // -------------------------------------------------------------------
  627. // State Feature
  628. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  629. {
  630. carla_debug("CarlaLv2Client::carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  631. CARLA_ASSERT(handle != nullptr);
  632. CARLA_ASSERT(path != nullptr);
  633. if (path == nullptr)
  634. return nullptr;
  635. QDir dir;
  636. dir.mkpath(path);
  637. return strdup(path);
  638. }
  639. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  640. {
  641. carla_debug("CarlaLv2Client::carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  642. CARLA_ASSERT(handle != nullptr);
  643. CARLA_ASSERT(absolute_path != nullptr);
  644. if (absolute_path == nullptr)
  645. return nullptr;
  646. QDir dir(absolute_path);
  647. return strdup(dir.canonicalPath().toUtf8().constData());
  648. }
  649. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  650. {
  651. carla_debug("CarlaLv2Client::carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  652. CARLA_ASSERT(handle != nullptr);
  653. CARLA_ASSERT(abstract_path != nullptr);
  654. if (abstract_path == nullptr)
  655. return nullptr;
  656. QDir dir(abstract_path);
  657. return strdup(dir.absolutePath().toUtf8().constData());
  658. }
  659. // -------------------------------------------------------------------
  660. // URI-Map Feature
  661. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  662. {
  663. carla_debug("CarlaLv2Client::carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  664. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  665. // unused
  666. (void)map;
  667. }
  668. // -------------------------------------------------------------------
  669. // URID Feature
  670. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  671. {
  672. CARLA_ASSERT(handle != nullptr);
  673. CARLA_ASSERT(uri != nullptr);
  674. carla_debug("CarlaLv2Client::carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  675. if (uri == nullptr)
  676. return CARLA_URI_MAP_ID_NULL;
  677. // Atom types
  678. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  679. return CARLA_URI_MAP_ID_ATOM_BLANK;
  680. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  681. return CARLA_URI_MAP_ID_ATOM_BOOL;
  682. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  683. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  684. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  685. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  686. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  687. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  688. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  689. return CARLA_URI_MAP_ID_ATOM_INT;
  690. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  691. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  692. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  693. return CARLA_URI_MAP_ID_ATOM_LONG;
  694. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  695. return CARLA_URI_MAP_ID_ATOM_PATH;
  696. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  697. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  698. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  699. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  700. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  701. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  702. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  703. return CARLA_URI_MAP_ID_ATOM_STRING;
  704. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  705. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  706. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  707. return CARLA_URI_MAP_ID_ATOM_URI;
  708. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  709. return CARLA_URI_MAP_ID_ATOM_URID;
  710. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  711. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  712. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  713. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  714. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  715. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  716. // BufSize types
  717. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  718. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  719. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  720. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  721. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  722. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  723. // Log types
  724. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  725. return CARLA_URI_MAP_ID_LOG_ERROR;
  726. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  727. return CARLA_URI_MAP_ID_LOG_NOTE;
  728. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  729. return CARLA_URI_MAP_ID_LOG_TRACE;
  730. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  731. return CARLA_URI_MAP_ID_LOG_WARNING;
  732. // Time types
  733. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  734. return CARLA_URI_MAP_ID_TIME_POSITION;
  735. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  736. return CARLA_URI_MAP_ID_TIME_BAR;
  737. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  738. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  739. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  740. return CARLA_URI_MAP_ID_TIME_BEAT;
  741. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  742. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  743. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  744. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  745. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  746. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  747. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  748. return CARLA_URI_MAP_ID_TIME_FRAME;
  749. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  750. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  751. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  752. return CARLA_URI_MAP_ID_TIME_SPEED;
  753. // Others
  754. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  755. return CARLA_URI_MAP_ID_MIDI_EVENT;
  756. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  757. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  758. if (handle == nullptr)
  759. return CARLA_URI_MAP_ID_NULL;
  760. // Custom types
  761. return ((CarlaLv2Client*)handle)->getCustomURID(uri);
  762. }
  763. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  764. {
  765. carla_debug("CarlaLv2Client::carla_lv2_urid_unmap(%p, %i)", handle, urid);
  766. CARLA_ASSERT(handle != nullptr);
  767. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  768. if (urid == CARLA_URI_MAP_ID_NULL)
  769. return nullptr;
  770. // Atom types
  771. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  772. return LV2_ATOM__Blank;
  773. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  774. return LV2_ATOM__Bool;
  775. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  776. return LV2_ATOM__Chunk;
  777. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  778. return LV2_ATOM__Double;
  779. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  780. return LV2_ATOM__Float;
  781. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  782. return LV2_ATOM__Int;
  783. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  784. return LV2_ATOM__Literal;
  785. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  786. return LV2_ATOM__Long;
  787. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  788. return LV2_ATOM__Path;
  789. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  790. return LV2_ATOM__Property;
  791. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  792. return LV2_ATOM__Resource;
  793. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  794. return LV2_ATOM__Sequence;
  795. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  796. return LV2_ATOM__String;
  797. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  798. return LV2_ATOM__Tuple;
  799. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  800. return LV2_ATOM__URI;
  801. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  802. return LV2_ATOM__URID;
  803. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  804. return LV2_ATOM__Vector;
  805. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  806. return LV2_ATOM__atomTransfer;
  807. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  808. return LV2_ATOM__eventTransfer;
  809. // BufSize types
  810. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  811. return LV2_BUF_SIZE__maxBlockLength;
  812. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  813. return LV2_BUF_SIZE__minBlockLength;
  814. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  815. return LV2_BUF_SIZE__sequenceSize;
  816. // Log types
  817. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  818. return LV2_LOG__Error;
  819. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  820. return LV2_LOG__Note;
  821. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  822. return LV2_LOG__Trace;
  823. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  824. return LV2_LOG__Warning;
  825. // Time types
  826. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  827. return LV2_TIME__Position;
  828. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  829. return LV2_TIME__bar;
  830. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  831. return LV2_TIME__barBeat;
  832. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  833. return LV2_TIME__beat;
  834. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  835. return LV2_TIME__beatUnit;
  836. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  837. return LV2_TIME__beatsPerBar;
  838. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  839. return LV2_TIME__beatsPerMinute;
  840. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  841. return LV2_TIME__frame;
  842. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  843. return LV2_TIME__framesPerSecond;
  844. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  845. return LV2_TIME__speed;
  846. // Others
  847. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  848. return LV2_MIDI__MidiEvent;
  849. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  850. return LV2_PARAMETERS__sampleRate;
  851. if (handle == nullptr)
  852. return nullptr;
  853. // Custom types
  854. return ((CarlaLv2Client*)handle)->getCustomURIString(urid);
  855. }
  856. // -------------------------------------------------------------------
  857. // UI Port-Map Feature
  858. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  859. {
  860. carla_debug("CarlaLv2Client::carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  861. CARLA_ASSERT(handle);
  862. if (handle == nullptr)
  863. return LV2UI_INVALID_PORT_INDEX;
  864. return ((CarlaLv2Client*)handle)->handleUiPortMap(symbol);
  865. }
  866. // -------------------------------------------------------------------
  867. // UI Resize Feature
  868. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  869. {
  870. carla_debug("CarlaLv2Client::carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  871. CARLA_ASSERT(handle != nullptr);
  872. if (handle == nullptr)
  873. return 1;
  874. return ((CarlaLv2Client*)handle)->handleUiResize(width, height);
  875. }
  876. // -------------------------------------------------------------------
  877. // UI Extension
  878. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  879. {
  880. CARLA_ASSERT(controller != nullptr);
  881. carla_debug("CarlaLv2Client::carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  882. if (controller == nullptr)
  883. return;
  884. ((CarlaLv2Client*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  885. }
  886. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaLv2Client)
  887. };
  888. #define lv2ClientPtr ((CarlaLv2Client*)fClient)
  889. int CarlaBridgeOsc::handleMsgLv2AtomTransfer(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  890. {
  891. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "is");
  892. carla_debug("CarlaBridgeOsc::handleMsgLv2AtomTransfer()");
  893. if (fClient == nullptr)
  894. return 1;
  895. const int32_t portIndex = argv[0]->i;
  896. const char* const atomBuf = (const char*)&argv[1]->s;
  897. if (portIndex < 0)
  898. return 0;
  899. QByteArray chunk;
  900. chunk = QByteArray::fromBase64(atomBuf);
  901. LV2_Atom* const atom = (LV2_Atom*)chunk.constData();
  902. lv2ClientPtr->handleAtomTransfer(portIndex, atom);
  903. return 0;
  904. }
  905. int CarlaBridgeOsc::handleMsgLv2UridMap(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  906. {
  907. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "is");
  908. carla_debug("CarlaBridgeOsc::handleMsgLv2UridMap()");
  909. if (fClient == nullptr)
  910. return 1;
  911. const int32_t urid = argv[0]->i;
  912. const char* const uri = (const char*)&argv[1]->s;
  913. if (urid <= 0)
  914. return 0;
  915. lv2ClientPtr->handleUridMap(urid, uri);
  916. return 0;
  917. }
  918. #undef lv2ClientPtr
  919. CARLA_BRIDGE_END_NAMESPACE
  920. int main(int argc, char* argv[])
  921. {
  922. CARLA_BRIDGE_USE_NAMESPACE
  923. if (argc != 5)
  924. {
  925. carla_stderr("usage: %s <osc-url|\"null\"> <plugin-uri> <ui-uri> <ui-title>", argv[0]);
  926. return 1;
  927. }
  928. const char* oscUrl = argv[1];
  929. const char* pluginURI = argv[2];
  930. const char* uiURI = argv[3];
  931. const char* uiTitle = argv[4];
  932. const bool useOsc(std::strcmp(oscUrl, "null") != 0);
  933. // try to get sampleRate value
  934. if (const char* const sampleRateStr = getenv("CARLA_SAMPLE_RATE"))
  935. gSampleRate = atof(sampleRateStr);
  936. // Init LV2 client
  937. CarlaLv2Client client(uiTitle);
  938. // Init OSC
  939. if (useOsc)
  940. client.oscInit(oscUrl);
  941. // Load UI
  942. int ret;
  943. if (client.uiInit(pluginURI, uiURI))
  944. {
  945. client.toolkitExec(!useOsc);
  946. ret = 0;
  947. }
  948. else
  949. {
  950. carla_stderr("Failed to load LV2 UI");
  951. ret = 1;
  952. }
  953. // Close OSC
  954. if (useOsc)
  955. client.oscClose();
  956. // Close LV2 client
  957. client.uiClose();
  958. return ret;
  959. }