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.

1256 lines
44KB

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