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.

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