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.

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