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.

1280 lines
45KB

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