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.

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