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.

1180 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. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  290. lv2World.load_all(); // FIXME
  291. fRdfDescriptor = lv2_rdf_new(pluginURI, false);
  292. if (fRdfDescriptor == nullptr)
  293. return false;
  294. // -----------------------------------------------------------------
  295. // find requested UI
  296. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  297. {
  298. if (std::strcmp(fRdfDescriptor->UIs[i].URI, uiURI) == 0)
  299. {
  300. fRdfUiDescriptor = &fRdfDescriptor->UIs[i];
  301. break;
  302. }
  303. }
  304. if (fRdfUiDescriptor == nullptr)
  305. {
  306. carla_stderr("Failed to find requested UI");
  307. return false;
  308. }
  309. // -----------------------------------------------------------------
  310. // open DLL
  311. if (! uiLibOpen(fRdfUiDescriptor->Binary))
  312. {
  313. carla_stderr("Failed to load UI binary, error was:\n%s", uiLibError());
  314. return false;
  315. }
  316. // -----------------------------------------------------------------
  317. // get DLL main entry
  318. const LV2UI_DescriptorFunction ui_descFn = (LV2UI_DescriptorFunction)uiLibSymbol("lv2ui_descriptor");
  319. if (ui_descFn == nullptr)
  320. return false;
  321. // -----------------------------------------------------------
  322. // get descriptor that matches URI
  323. uint32_t i = 0;
  324. while ((fDescriptor = ui_descFn(i++)))
  325. {
  326. if (std::strcmp(fDescriptor->URI, uiURI) == 0)
  327. break;
  328. }
  329. if (fDescriptor == nullptr)
  330. {
  331. carla_stderr("Failed to find UI descriptor");
  332. return false;
  333. }
  334. // -----------------------------------------------------------
  335. // initialize UI
  336. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  337. fFeatures[kFeatureIdUiParent]->data = getContainerId();
  338. #endif
  339. fHandle = fDescriptor->instantiate(fDescriptor, pluginURI, fRdfUiDescriptor->Bundle, carla_lv2_ui_write_function, this, &fWidget, fFeatures);
  340. if (fHandle == nullptr)
  341. {
  342. carla_stderr("Failed to init UI");
  343. return false;
  344. }
  345. // -----------------------------------------------------------
  346. // check if not resizable
  347. for (uint32_t i=0; i < fRdfUiDescriptor->FeatureCount && fIsResizable; ++i)
  348. {
  349. if (std::strcmp(fRdfUiDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0 || std::strcmp(fRdfUiDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  350. {
  351. fIsResizable = false;
  352. break;
  353. }
  354. }
  355. // -----------------------------------------------------------
  356. // check for known extensions
  357. if (fDescriptor->extension_data != nullptr)
  358. {
  359. fExt.programs = (const LV2_Programs_UI_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__UIInterface);
  360. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  361. fExt.idle = (const LV2UI_Idle_Interface*)fDescriptor->extension_data(LV2_UI__idleInterface);
  362. // check if invalid
  363. if (fExt.programs != nullptr && fExt.programs->select_program == nullptr)
  364. fExt.programs = nullptr;
  365. if (fExt.idle != nullptr && fExt.idle->idle == nullptr)
  366. fExt.idle = nullptr;
  367. }
  368. return true;
  369. }
  370. void uiIdle() override
  371. {
  372. if (fHandle != nullptr && fExt.idle != nullptr)
  373. fExt.idle->idle(fHandle);
  374. }
  375. void uiClose() override
  376. {
  377. CarlaBridgeClient::uiClose();
  378. if (fHandle && fDescriptor && fDescriptor->cleanup)
  379. fDescriptor->cleanup(fHandle);
  380. uiLibClose();
  381. }
  382. // ---------------------------------------------------------------------
  383. // ui management
  384. void* getWidget() const override
  385. {
  386. return fWidget;
  387. }
  388. bool isResizable() const override
  389. {
  390. return fIsResizable;
  391. }
  392. bool needsReparent() const override
  393. {
  394. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  395. return true;
  396. #else
  397. return false;
  398. #endif
  399. }
  400. // ---------------------------------------------------------------------
  401. // ui processing
  402. void setParameter(const int32_t rindex, const float value) override
  403. {
  404. CARLA_ASSERT(fHandle != nullptr && fDescriptor != nullptr);
  405. if (fHandle != nullptr && fDescriptor != nullptr && fDescriptor->port_event != nullptr)
  406. fDescriptor->port_event(fHandle, rindex, sizeof(float), 0, &value);
  407. }
  408. void setProgram(const uint32_t) override
  409. {
  410. }
  411. void setMidiProgram(const uint32_t bank, const uint32_t program) override
  412. {
  413. CARLA_ASSERT(fHandle != nullptr);
  414. if (fHandle != nullptr && fExt.programs != nullptr)
  415. fExt.programs->select_program(fHandle, bank, program);
  416. }
  417. void noteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  418. {
  419. CARLA_ASSERT(fHandle != nullptr && fDescriptor != nullptr);
  420. if (fHandle != nullptr && fDescriptor != nullptr && fDescriptor->port_event != nullptr)
  421. {
  422. LV2_Atom_MidiEvent midiEv;
  423. midiEv.event.time.frames = 0;
  424. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  425. midiEv.event.body.size = 3;
  426. midiEv.data[0] = MIDI_STATUS_NOTE_ON + channel;
  427. midiEv.data[1] = note;
  428. midiEv.data[2] = velo;
  429. fDescriptor->port_event(fHandle, 0, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  430. }
  431. }
  432. void noteOff(const uint8_t channel, const uint8_t note) override
  433. {
  434. CARLA_ASSERT(fHandle != nullptr && fDescriptor != nullptr);
  435. if (fHandle != nullptr && fDescriptor != nullptr && fDescriptor->port_event != nullptr)
  436. {
  437. LV2_Atom_MidiEvent midiEv;
  438. midiEv.event.time.frames = 0;
  439. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  440. midiEv.event.body.size = 3;
  441. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  442. midiEv.data[1] = note;
  443. midiEv.data[2] = 0;
  444. fDescriptor->port_event(fHandle, 0, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  445. }
  446. }
  447. // ---------------------------------------------------------------------
  448. LV2_URID getCustomURID(const char* const uri)
  449. {
  450. CARLA_ASSERT(uri != nullptr);
  451. carla_debug("CarlaLv2Client::getCustomURID(\"%s\")", uri);
  452. if (uri == nullptr)
  453. return CARLA_URI_MAP_ID_NULL;
  454. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  455. {
  456. const char* const thisUri(fCustomURIDs.getAt(i));
  457. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  458. return i;
  459. }
  460. fCustomURIDs.append(carla_strdup(uri));
  461. const LV2_URID urid(fCustomURIDs.count()-1);
  462. if (isOscControlRegistered())
  463. sendOscLv2UridMap(urid, uri);
  464. return urid;
  465. }
  466. const char* getCustomURIString(const LV2_URID urid)
  467. {
  468. CARLA_ASSERT(urid != CARLA_URI_MAP_ID_NULL);
  469. CARLA_ASSERT_INT2(urid < fCustomURIDs.count(), urid, fCustomURIDs.count());
  470. carla_debug("CarlaLv2Client::getCustomURIString(%i)", urid);
  471. if (urid == CARLA_URI_MAP_ID_NULL)
  472. return nullptr;
  473. if (urid < fCustomURIDs.count())
  474. return fCustomURIDs.getAt(urid);
  475. return nullptr;
  476. }
  477. // ---------------------------------------------------------------------
  478. void handleProgramChanged(const int32_t /*index*/)
  479. {
  480. if (isOscControlRegistered())
  481. sendOscConfigure("reloadprograms", "");
  482. }
  483. uint32_t handleUiPortMap(const char* const symbol)
  484. {
  485. CARLA_ASSERT(symbol != nullptr);
  486. if (symbol == nullptr)
  487. return LV2UI_INVALID_PORT_INDEX;
  488. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  489. {
  490. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  491. return i;
  492. }
  493. return LV2UI_INVALID_PORT_INDEX;
  494. }
  495. int handleUiResize(const int width, const int height)
  496. {
  497. CARLA_ASSERT(width > 0);
  498. CARLA_ASSERT(height > 0);
  499. if (width <= 0 || height <= 0)
  500. return 1;
  501. toolkitResize(width, height);
  502. return 0;
  503. }
  504. void handleUiWrite(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  505. {
  506. if (format == 0)
  507. {
  508. CARLA_ASSERT(buffer != nullptr);
  509. CARLA_ASSERT(bufferSize == sizeof(float));
  510. if (bufferSize != sizeof(float))
  511. return;
  512. if (buffer == nullptr || bufferSize != sizeof(float))
  513. return;
  514. const float value(*(const float*)buffer);
  515. if (isOscControlRegistered())
  516. sendOscControl(portIndex, value);
  517. }
  518. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM || CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  519. {
  520. CARLA_ASSERT(bufferSize != 0);
  521. CARLA_ASSERT(buffer != nullptr);
  522. if (bufferSize == 0 || buffer == nullptr)
  523. return;
  524. if (isOscControlRegistered())
  525. sendOscLv2AtomTransfer(portIndex, QByteArray((const char*)buffer, bufferSize).toBase64().constData());
  526. }
  527. else
  528. {
  529. carla_stdout("CarlaLv2Client::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", portIndex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  530. }
  531. }
  532. // ---------------------------------------------------------------------
  533. void handleAtomTransfer(const uint32_t portIndex, const LV2_Atom* const atom)
  534. {
  535. CARLA_ASSERT(atom != nullptr);
  536. carla_debug("CarlaLv2Client::handleTransferEvent(%i, %p)", portIndex, atom);
  537. if (atom != nullptr && fHandle != nullptr && fDescriptor != nullptr && fDescriptor->port_event != nullptr)
  538. fDescriptor->port_event(fHandle, portIndex, lv2_atom_total_size(atom), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  539. }
  540. void handleUridMap(const LV2_URID urid, const char* const uri)
  541. {
  542. CARLA_ASSERT(urid != CARLA_URI_MAP_ID_NULL);
  543. CARLA_ASSERT(uri != nullptr);
  544. carla_debug("CarlaLv2Client::handleUridMap(%i, \"%s\")", urid, uri);
  545. // TODO
  546. }
  547. private:
  548. LV2UI_Handle fHandle;
  549. LV2UI_Widget fWidget;
  550. LV2_Feature* fFeatures[kFeatureCount+1];
  551. const LV2UI_Descriptor* fDescriptor;
  552. const LV2_RDF_Descriptor* fRdfDescriptor;
  553. const LV2_RDF_UI* fRdfUiDescriptor;
  554. Lv2PluginOptions fOptions;
  555. bool fIsResizable;
  556. LinkedList<const char*> fCustomURIDs;
  557. struct Extensions {
  558. const LV2_Options_Interface* options;
  559. const LV2UI_Idle_Interface* idle;
  560. const LV2_Programs_UI_Interface* programs;
  561. Extensions()
  562. : options(nullptr),
  563. idle(nullptr),
  564. programs(nullptr) {}
  565. } fExt;
  566. // -------------------------------------------------------------------
  567. // Logs Feature
  568. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  569. {
  570. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  571. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  572. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  573. #ifndef DEBUG
  574. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  575. return 0;
  576. #endif
  577. va_list args;
  578. va_start(args, fmt);
  579. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  580. va_end(args);
  581. return ret;
  582. }
  583. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  584. {
  585. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  586. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  587. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  588. #ifndef DEBUG
  589. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  590. return 0;
  591. #endif
  592. int ret = 0;
  593. switch (type)
  594. {
  595. case CARLA_URI_MAP_ID_LOG_ERROR:
  596. std::fprintf(stderr, "\x1b[31m");
  597. ret = std::vfprintf(stderr, fmt, ap);
  598. std::fprintf(stderr, "\x1b[0m");
  599. break;
  600. case CARLA_URI_MAP_ID_LOG_NOTE:
  601. ret = std::vfprintf(stdout, fmt, ap);
  602. break;
  603. case CARLA_URI_MAP_ID_LOG_TRACE:
  604. #ifdef DEBUG
  605. std::fprintf(stdout, "\x1b[30;1m");
  606. ret = std::vfprintf(stdout, fmt, ap);
  607. std::fprintf(stdout, "\x1b[0m");
  608. #endif
  609. break;
  610. case CARLA_URI_MAP_ID_LOG_WARNING:
  611. ret = std::vfprintf(stderr, fmt, ap);
  612. break;
  613. default:
  614. break;
  615. }
  616. return ret;
  617. }
  618. // -------------------------------------------------------------------
  619. // Programs Feature
  620. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  621. {
  622. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  623. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  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_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  631. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  632. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  633. QDir dir;
  634. dir.mkpath(path);
  635. return strdup(path);
  636. }
  637. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  638. {
  639. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  640. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', nullptr);
  641. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  642. QDir dir(absolute_path);
  643. return strdup(dir.canonicalPath().toUtf8().constData());
  644. }
  645. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  646. {
  647. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  648. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', nullptr);
  649. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  650. QDir dir(abstract_path);
  651. return strdup(dir.absolutePath().toUtf8().constData());
  652. }
  653. // -------------------------------------------------------------------
  654. // URI-Map Feature
  655. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  656. {
  657. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  658. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  659. // unused
  660. (void)map;
  661. }
  662. // -------------------------------------------------------------------
  663. // URID Feature
  664. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  665. {
  666. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, CARLA_URI_MAP_ID_NULL);
  667. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  668. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  669. // Atom types
  670. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  671. return CARLA_URI_MAP_ID_ATOM_BLANK;
  672. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  673. return CARLA_URI_MAP_ID_ATOM_BOOL;
  674. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  675. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  676. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  677. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  678. if (std::strcmp(uri, LV2_ATOM__Event) == 0)
  679. return CARLA_URI_MAP_ID_ATOM_EVENT;
  680. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  681. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  682. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  683. return CARLA_URI_MAP_ID_ATOM_INT;
  684. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  685. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  686. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  687. return CARLA_URI_MAP_ID_ATOM_LONG;
  688. if (std::strcmp(uri, LV2_ATOM__Number) == 0)
  689. return CARLA_URI_MAP_ID_ATOM_NUMBER;
  690. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  691. return CARLA_URI_MAP_ID_ATOM_OBJECT;
  692. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  693. return CARLA_URI_MAP_ID_ATOM_PATH;
  694. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  695. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  696. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  697. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  698. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  699. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  700. if (std::strcmp(uri, LV2_ATOM__Sound) == 0)
  701. return CARLA_URI_MAP_ID_ATOM_SOUND;
  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. // Custom types
  759. return ((CarlaLv2Client*)handle)->getCustomURID(uri);
  760. }
  761. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  762. {
  763. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  764. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  765. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  766. // Atom types
  767. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  768. return LV2_ATOM__Blank;
  769. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  770. return LV2_ATOM__Bool;
  771. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  772. return LV2_ATOM__Chunk;
  773. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  774. return LV2_ATOM__Double;
  775. if (urid == CARLA_URI_MAP_ID_ATOM_EVENT)
  776. return LV2_ATOM__Event;
  777. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  778. return LV2_ATOM__Float;
  779. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  780. return LV2_ATOM__Int;
  781. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  782. return LV2_ATOM__Literal;
  783. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  784. return LV2_ATOM__Long;
  785. if (urid == CARLA_URI_MAP_ID_ATOM_NUMBER)
  786. return LV2_ATOM__Number;
  787. if (urid == CARLA_URI_MAP_ID_ATOM_OBJECT)
  788. return LV2_ATOM__Object;
  789. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  790. return LV2_ATOM__Path;
  791. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  792. return LV2_ATOM__Property;
  793. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  794. return LV2_ATOM__Resource;
  795. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  796. return LV2_ATOM__Sequence;
  797. if (urid == CARLA_URI_MAP_ID_ATOM_SOUND)
  798. return LV2_ATOM__Sound;
  799. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  800. return LV2_ATOM__String;
  801. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  802. return LV2_ATOM__Tuple;
  803. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  804. return LV2_ATOM__URI;
  805. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  806. return LV2_ATOM__URID;
  807. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  808. return LV2_ATOM__Vector;
  809. if (urid == CARLA_URI_MAP_ID_ATOM_WORKER)
  810. return nullptr; // custom
  811. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  812. return LV2_ATOM__atomTransfer;
  813. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  814. return LV2_ATOM__eventTransfer;
  815. // BufSize types
  816. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  817. return LV2_BUF_SIZE__maxBlockLength;
  818. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  819. return LV2_BUF_SIZE__minBlockLength;
  820. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  821. return LV2_BUF_SIZE__sequenceSize;
  822. // Log types
  823. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  824. return LV2_LOG__Error;
  825. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  826. return LV2_LOG__Note;
  827. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  828. return LV2_LOG__Trace;
  829. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  830. return LV2_LOG__Warning;
  831. // Time types
  832. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  833. return LV2_TIME__Position;
  834. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  835. return LV2_TIME__bar;
  836. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  837. return LV2_TIME__barBeat;
  838. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  839. return LV2_TIME__beat;
  840. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  841. return LV2_TIME__beatUnit;
  842. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  843. return LV2_TIME__beatsPerBar;
  844. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  845. return LV2_TIME__beatsPerMinute;
  846. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  847. return LV2_TIME__frame;
  848. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  849. return LV2_TIME__framesPerSecond;
  850. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  851. return LV2_TIME__speed;
  852. // Others
  853. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  854. return LV2_MIDI__MidiEvent;
  855. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  856. return LV2_PARAMETERS__sampleRate;
  857. // Custom types
  858. return ((CarlaLv2Client*)handle)->getCustomURIString(urid);
  859. }
  860. // -------------------------------------------------------------------
  861. // UI Port-Map Feature
  862. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  863. {
  864. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  865. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  866. return ((CarlaLv2Client*)handle)->handleUiPortMap(symbol);
  867. }
  868. // -------------------------------------------------------------------
  869. // UI Resize Feature
  870. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  871. {
  872. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  873. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  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_SAFE_ASSERT_RETURN(controller != nullptr,);
  881. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  882. ((CarlaLv2Client*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  883. }
  884. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaLv2Client)
  885. };
  886. #define lv2ClientPtr ((CarlaLv2Client*)fClient)
  887. int CarlaBridgeOsc::handleMsgLv2AtomTransfer(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  888. {
  889. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "is");
  890. carla_debug("CarlaBridgeOsc::handleMsgLv2AtomTransfer()");
  891. if (fClient == nullptr)
  892. return 1;
  893. const int32_t portIndex = argv[0]->i;
  894. const char* const atomBuf = (const char*)&argv[1]->s;
  895. if (portIndex < 0)
  896. return 0;
  897. QByteArray chunk;
  898. chunk = QByteArray::fromBase64(atomBuf);
  899. LV2_Atom* const atom = (LV2_Atom*)chunk.constData();
  900. lv2ClientPtr->handleAtomTransfer(portIndex, atom);
  901. return 0;
  902. }
  903. int CarlaBridgeOsc::handleMsgLv2UridMap(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  904. {
  905. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "is");
  906. carla_debug("CarlaBridgeOsc::handleMsgLv2UridMap()");
  907. if (fClient == nullptr)
  908. return 1;
  909. const int32_t urid = argv[0]->i;
  910. const char* const uri = (const char*)&argv[1]->s;
  911. if (urid <= 0)
  912. return 0;
  913. lv2ClientPtr->handleUridMap(urid, uri);
  914. return 0;
  915. }
  916. #undef lv2ClientPtr
  917. CARLA_BRIDGE_END_NAMESPACE
  918. int main(int argc, char* argv[])
  919. {
  920. CARLA_BRIDGE_USE_NAMESPACE
  921. if (argc != 5)
  922. {
  923. carla_stderr("usage: %s <osc-url|\"null\"> <plugin-uri> <ui-uri> <ui-title>", argv[0]);
  924. return 1;
  925. }
  926. const char* oscUrl = argv[1];
  927. const char* pluginURI = argv[2];
  928. const char* uiURI = argv[3];
  929. const char* uiTitle = argv[4];
  930. const bool useOsc(std::strcmp(oscUrl, "null") != 0);
  931. // try to get sampleRate value
  932. if (const char* const sampleRateStr = getenv("CARLA_SAMPLE_RATE"))
  933. gSampleRate = atof(sampleRateStr);
  934. // Init LV2 client
  935. CarlaLv2Client client(uiTitle);
  936. // Init OSC
  937. if (useOsc)
  938. client.oscInit(oscUrl);
  939. // Load UI
  940. int ret;
  941. if (client.uiInit(pluginURI, uiURI))
  942. {
  943. client.toolkitExec(!useOsc);
  944. ret = 0;
  945. }
  946. else
  947. {
  948. ret = 1;
  949. }
  950. // Close OSC
  951. if (useOsc)
  952. client.oscClose();
  953. // Close LV2 client
  954. client.uiClose();
  955. return ret;
  956. }